Skip to content
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

[#7176] json.h update #7181

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions src/support/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,26 @@ struct Value {
while (*curr && is_json_space(*curr)) \
curr++; \
}
#define skip_escaped_characters(ptr) \
while (*ptr && *ptr != '"') { \
if (*ptr == '\\' && *(ptr + 1)) { \
ptr++; \
} \
ptr++; \
}
#define RUNTIME_ASSERT(condition) \
if (!(condition)) { \
std::cerr << "Assertion failed: " #condition << " at " << __FILE__ << ":" \
<< __LINE__ << "\n"; \
std::terminate(); \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have Fatal for this in utilities.h, so we can include that and then replace these two lines with Fatal() << "..message..";

Also I think it is best to avoid a C macro for this, and just do if (..) { Fatal() .. in the code (the downside of a macro is it isn't clear from the name if it is just an assert - and hence disabled in non-assertion builds - etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}
skip();
if (*curr == '"') {
// String
curr++;
char* close = strchr(curr, '"');
assert(close);
char* close = curr;
skip_escaped_characters(close);
RUNTIME_ASSERT(*close == '"');
*close = 0; // end this string, and reuse it straight from the input
setString(curr);
curr = close + 1;
Expand Down Expand Up @@ -305,20 +319,35 @@ struct Value {
skip();
setObject();
while (*curr != '}') {
assert(*curr == '"');
curr++;
char* close = strchr(curr, '"');
assert(close);
*close = 0; // end this string, and reuse it straight from the input
IString key(curr);
curr = close + 1;
skip();
assert(*curr == ':');
curr++;
skip();
Ref value = Ref(new Value());
curr = value->parse(curr);
(*obj)[key] = value;
if (*curr == '"') {
curr++;
char* close = curr;
skip_escaped_characters(close);
RUNTIME_ASSERT(*close == '"');
*close = 0; // end this string, and reuse it straight from the input
IString key(curr);
curr = close + 1;
skip();
assert(*curr == ':');
curr++;
skip();
Ref value = Ref(new Value());
curr = value->parse(curr);
(*obj)[key] = value;
} else {
// Unquoted key
char* start = curr;
while (*curr && *curr != ':' && !is_json_space(*curr)) {
curr++;
}
assert(*curr == ':');
IString key(std::string(start, curr - start).c_str());
curr++;
skip();
Ref value = Ref(new Value());
curr = value->parse(curr);
(*obj)[key] = value;
}
skip();
if (*curr == '}') {
break;
Expand Down
34 changes: 34 additions & 0 deletions test/lit/metadce/functions_with_names_need_escape.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-metadce %s --graph-file %s.json -S -o - | filecheck %s --check-prefix=TXT
;; RUN: wasm-as %s -o %t.wasm --source-map %t.map
;; RUN: wasm-metadce %t.wasm --input-source-map %t.map --graph-file %s.json -o %t.out.wasm --output-source-map %t.out.map
;; RUN: wasm-dis %t.out.wasm --source-map %t.out.map -o - | filecheck %s --check-prefix=BIN

;; Test that sourcemap information is preserved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment looks like it was copied from another test perhaps?

Copy link
Contributor Author

@mtb0x1 mtb0x1 Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I removed not need stuff. test files should be minimal by now.


(module
;;@ a:1:2
;; TXT: (type $0 (func))

;; TXT: (export "f" (func $f))

;; TXT: (func $f
;; TXT-NEXT: ;;@ a:7:8:someSymbol
;; TXT-NEXT: (nop)
;; TXT-NEXT: ;;@ a:9:10
;; TXT-NEXT: )
(func $f (export "f")
;;@ a:7:8:someSymbol
mtb0x1 marked this conversation as resolved.
Show resolved Hide resolved
(nop)
;;@ a:9:10
)
)
;; BIN: (type $0 (func))

;; BIN: (export "f" (func $0))

;; BIN: (func $0
;; BIN-NEXT: ;;@ a:7:8:someSymbol
;; BIN-NEXT: (nop)
;; BIN-NEXT: ;;@ a:9:10
;; BIN-NEXT: )
13 changes: 13 additions & 0 deletions test/lit/metadce/functions_with_names_need_escape.wat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"name": "root",
"reaches": [
"f","f~!@#$%^&*()_+`-={}|[]\\:\";'<>?,./"
],
"root": true
},
{
"name": "f",
"export": "f"
}
]
34 changes: 34 additions & 0 deletions test/lit/metadce/keys_quoted_unquoted.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-metadce %s --graph-file %s.json -S -o - | filecheck %s --check-prefix=TXT
;; RUN: wasm-as %s -o %t.wasm --source-map %t.map
;; RUN: wasm-metadce %t.wasm --input-source-map %t.map --graph-file %s.json -o %t.out.wasm --output-source-map %t.out.map
;; RUN: wasm-dis %t.out.wasm --source-map %t.out.map -o - | filecheck %s --check-prefix=BIN

;; Test that sourcemap information is preserved

(module
;;@ a:1:2
;; TXT: (type $0 (func))

;; TXT: (export "f" (func $f))

;; TXT: (func $f
;; TXT-NEXT: ;;@ a:7:8:someSymbol
;; TXT-NEXT: (nop)
;; TXT-NEXT: ;;@ a:9:10
;; TXT-NEXT: )
(func $f (export "f")
;;@ a:7:8:someSymbol
(nop)
;;@ a:9:10
)
)
;; BIN: (type $0 (func))

;; BIN: (export "f" (func $0))

;; BIN: (func $0
;; BIN-NEXT: ;;@ a:7:8:someSymbol
;; BIN-NEXT: (nop)
;; BIN-NEXT: ;;@ a:9:10
;; BIN-NEXT: )
13 changes: 13 additions & 0 deletions test/lit/metadce/keys_quoted_unquoted.wat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
name: "root",
reaches: [
"f"
],
root: true
},
{
"name": "f",
"export": "f"
}
]
Loading