Skip to content

Commit

Permalink
🐞 fix: #376 Fix default export
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Aug 4, 2024
1 parent 6db400a commit 098105e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/release_notes/release_notes_3_1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Release Notes 3.1.x
===================

3.1.5 V8 v12.8
--------------

* Fixed a bug of the default export in ``JavetBuiltInModuleResolver``

3.1.4 V8 v12.7
--------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ public IV8Module resolve(V8Runtime v8Runtime, String resourceName, IV8Module v8M
IV8Module iV8Module = null;
// It only works for Node.js runtime and module names starting with "node:".
if (v8Runtime.getJSRuntimeType().isNode() && resourceName != null && resourceName.startsWith(PREFIX_NODE)) {
String moduleName = resourceName.substring(PREFIX_NODE.length());
NodeRuntime nodeRuntime = (NodeRuntime) v8Runtime;
NodeModuleAny nodeModuleAny = nodeRuntime.getNodeModule(moduleName, NodeModuleAny.class);
NodeModuleAny nodeModuleAny = nodeRuntime.getNodeModule(resourceName, NodeModuleAny.class);
V8ValueObject v8ValueObject = nodeModuleAny.getModuleObject();
if (!v8ValueObject.has(DEFAULT)) {
v8ValueObject.set(DEFAULT, v8ValueObject);
}
/*
* https://github.com/caoccao/Javet/issues/376
*
* Sometimes the module object has 'default' property, but that property seems to be weird.
* Resetting the 'default' property resolves the issue.
*/
v8ValueObject.set(DEFAULT, v8ValueObject);
iV8Module = v8Runtime.createV8Module(resourceName, v8ValueObject);
}
return iV8Module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,13 @@ public void testJavetBuiltInModuleResolverWithDefault() throws JavetException {
v8Runtime.setV8ModuleResolver(new JavetBuiltInModuleResolver());
v8Runtime.getExecutor(
"import fs from 'node:fs';\n" +
"import events from 'node:events';\n" +
"import stream from 'node:stream';\n" +
"globalThis.b = events === stream;\n" +
"globalThis.a = fs.existsSync('/path-not-found');")
.setModule(true).executeVoid();
assertFalse(v8Runtime.getGlobalObject().getBoolean("a"));
assertFalse(v8Runtime.getGlobalObject().getBoolean("b"));
}
}

Expand Down

0 comments on commit 098105e

Please sign in to comment.