-
Notifications
You must be signed in to change notification settings - Fork 40
escape data strings according to WebAssembly spec , Fixes #37 #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,28 @@ type indice = | |
|
|
||
| let pp_id fmt id = pf fmt "$%s" id | ||
|
|
||
| let pp_name_inner fmt s = | ||
| let pp_hex_char fmt c = pf fmt "\\%02x" (Char.code c) in | ||
| let pp_char fmt = function | ||
| | '\n' -> string fmt "\\n" | ||
| | '\r' -> string fmt "\\r" | ||
| | '\t' -> string fmt "\\t" | ||
| | '\'' -> string fmt "\\'" | ||
| | '"' -> string fmt "\\\"" | ||
| | '\\' -> string fmt "\\\\" | ||
| | c -> | ||
| let ci = Char.code c in | ||
| if 0x20 <= ci && ci < 0x7f then char fmt c else pp_hex_char fmt c | ||
| in | ||
| let pp_unicode_char fmt = function | ||
| | (0x09 | 0x0a) as c -> pp_char fmt (Char.chr c) | ||
| | uc when 0x20 <= uc && uc < 0x7f -> pp_char fmt (Char.chr uc) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you could use a character interval for the pattern (and it's likely not necessary to use integers for this function, using char should be enough) |
||
| | uc -> pf fmt "\\u{%02x}" uc | ||
| in | ||
| String.iter (fun c -> pp_unicode_char fmt (Char.code c)) s | ||
|
|
||
| let pp_name fmt s = pf fmt {|"%a"|} pp_name_inner s | ||
|
|
||
| let pp_id_opt fmt = function None -> () | Some i -> pf fmt " %a" pp_id i | ||
|
|
||
| let pp_indice fmt = function Raw u -> int fmt u | Text i -> pp_id fmt i | ||
|
|
@@ -761,7 +783,7 @@ module Data = struct | |
| } | ||
|
|
||
| let pp fmt (d : t) = | ||
| pf fmt {|(data%a %a %S)|} pp_id_opt d.id Mode.pp d.mode d.init | ||
| pf fmt {|(data%a %a %a)|} pp_id_opt d.id Mode.pp d.mode pp_name d.init | ||
| end | ||
|
|
||
| module Tag = struct | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (module | ||
| (memory 1) | ||
| (data (i32.const 0) "Hello\00World\01\02\03\ff") | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| test data special chars round-trip: | ||
| $ owi fmt data_special_chars.wat > /tmp/owi_test_output.wat | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting the file in |
||
| $ owi fmt /tmp/owi_test_output.wat | ||
| (module | ||
| (memory 1) | ||
| (data (memory 0) (offset i32.const 0) "hello\n\t\u{0d}\"\'\\world") | ||
| ) | ||
| $ rm /tmp/owi_test_output.wat | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to do this if you put it in the current directory |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (module | ||
| (memory 1) | ||
| (data (i32.const 0) "hello\n\t\r\"'\\world") | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,5 +11,7 @@ | |
| m.wat | ||
| locals.wat | ||
| locals_drop.wat | ||
| data_special_chars.wat | ||
| data_bytes.wat | ||
| script.wast | ||
| script.t)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be rewritten as: