Skip to content

Commit 9750ca5

Browse files
authored
[esm-integration] Fix some tests that did't run in strict JS mode (#24297)
In strict JS mode all variables need to be declared. In addition this change fixes an error where `wasmTable` was being both delared and imported via an ES6 import.
1 parent b1371c6 commit 9750ca5

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,8 @@ jobs:
803803
core0.test_cubescript_jspi
804804
esm_integration.test_fs_js_api*
805805
instance.test_hello_world
806+
esm_integration.test_inlinejs3
807+
esm_integration.test_embind_val_basics
806808
"
807809
# Run some basic tests with the minimum version of node that we currently
808810
# support in the generated code.

src/jsifier.mjs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,21 @@ function(${args}) {
721721
// emits
722722
// 'var foo;[code here verbatim];'
723723
contentText = 'var ' + mangled + snippet;
724-
if (snippet[snippet.length - 1] != ';' && snippet[snippet.length - 1] != '}')
724+
if (snippet[snippet.length - 1] != ';' && snippet[snippet.length - 1] != '}') {
725725
contentText += ';';
726+
}
726727
} else if (typeof snippet == 'undefined') {
727-
contentText = `var ${mangled};`;
728+
// wasmTable is kind of special. In the normal configuration we export
729+
// it from the wasm module under the name `__indirect_function_table`
730+
// but we declare it as an 'undefined'. It then gets assigned manually
731+
// once the wasm module is available.
732+
// TODO(sbc): This is kind of hacky, we should come up with a better solution.
733+
var isDirectWasmExport = WASM_ESM_INTEGRATION && mangled == 'wasmTable';
734+
if (isDirectWasmExport) {
735+
contentText = '';
736+
} else {
737+
contentText = `var ${mangled};`;
738+
}
728739
} else {
729740
// In JS libraries
730741
// foo: '=[value]'

test/core/test_inlinejs3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int main(int argc, char **argv) {
2828
},
2929
i, (double)i / 12);
3030
}
31-
EM_ASM_INT({ globalVar = $0 }, sum); // no outputs, just input
31+
EM_ASM_INT({ globalThis.globalVar = $0 }, sum); // no outputs, just input
3232
sum = 0;
3333
sum = EM_ASM_INT(return globalVar); // no inputs, just output
3434
printf("sum: %d\n", sum);

test/embind/test_val.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,15 @@ int main() {
164164

165165
test("bool isNull()");
166166
EM_ASM(
167-
a = null;
168-
b = false;
167+
globalThis.a = null;
168+
globalThis.b = false;
169+
globalThis.c = null;
170+
globalThis.d = null;
171+
globalThis.e = null;
172+
globalThis.f = null;
173+
globalThis.g = null;
174+
globalThis.h = null;
175+
globalThis.i = null;
169176
);
170177
ensure(val::global("a").isNull());
171178
ensure_not(val::global("b").isNull());
@@ -384,15 +391,15 @@ int main() {
384391

385392
test("template<typename... Args> val new_(Args&&... args)");
386393
EM_ASM(
387-
A = function() {
394+
globalThis.A = function() {
388395
this.value = 2;
389396
}
390397
);
391398
val::global().set("a", val::global("A").new_());
392399
ensure_js("a instanceof A");
393400
ensure_js("a.value == 2");
394401
EM_ASM(
395-
A = function(arg1, arg2) {
402+
globalThis.A = function(arg1, arg2) {
396403
this.arg1 = arg1;
397404
this.arg2 = arg2;
398405
}
@@ -441,10 +448,10 @@ int main() {
441448
ensure(val::global("f")().as<int>() == 2);
442449
ensure_not(val::global("f")().as<int>() == 3);
443450
EM_ASM(
444-
f1 = function(arg1, arg2) {
451+
globalThis.f1 = function(arg1, arg2) {
445452
return arg1;
446453
};
447-
f2 = function(arg1, arg2) {
454+
globalThis.f2 = function(arg1, arg2) {
448455
return arg2;
449456
};
450457
);
@@ -453,14 +460,14 @@ int main() {
453460

454461
test("template<typename ReturnValue, typename... Args> ReturnValue call(const char* name, Args&&... args)");
455462
EM_ASM(
456-
C = function() {
463+
globalThis.C = function() {
457464
this.method = function() { return this; };
458465
};
459466
c = new C;
460467
);
461468
ensure(val::global("c").call<val>("method") == val::global("c"));
462469
EM_ASM(
463-
C = function() {
470+
globalThis.C = function() {
464471
this.method = function(arg) { return arg; };
465472
};
466473
c = new C;
@@ -536,8 +543,8 @@ int main() {
536543

537544
test("bool instanceof(const val& v)");
538545
EM_ASM(
539-
A = function() {};
540-
B = function() {};
546+
globalThis.A = function() {};
547+
globalThis.B = function() {};
541548
a = new A;
542549
);
543550
ensure(val::global("a").instanceof(val::global("A")));
@@ -583,15 +590,11 @@ int main() {
583590

584591
test("void throw_() const");
585592
EM_ASM(
586-
test_val_throw_ = function(error)
587-
{
588-
try
589-
{
593+
globalThis.test_val_throw_ = function(error) {
594+
try {
590595
Module.throw_js_error(error);
591596
return false;
592-
}
593-
catch(error_thrown)
594-
{
597+
} catch(error_thrown) {
595598
if (error_thrown != error)
596599
throw error_thrown;
597600
}

0 commit comments

Comments
 (0)