Skip to content

Commit

Permalink
accept to way to differentiate the way to globalize imported members
Browse files Browse the repository at this point in the history
  • Loading branch information
hmsk committed Nov 7, 2024
1 parent e33f4b8 commit 3754c68
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ext/quickjsrb/quickjsrb.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,14 @@ static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
rb_scan_args(argc, argv, "10:", &r_import_string, &r_opts);
if (NIL_P(r_opts))
r_opts = rb_hash_new();
VALUE r_from = rb_hash_aref(r_opts, ID2SYM(rb_intern("from"))); // TODO: Use kwargs instead
VALUE r_from = rb_hash_aref(r_opts, ID2SYM(rb_intern("from")));
if (NIL_P(r_from))
{
VALUE r_error_message = rb_str_new2("missing import source");
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, r_error_message, Qnil));
return Qnil;
}
VALUE r_custom_exposure = rb_hash_aref(r_opts, ID2SYM(rb_intern("code_to_expose")));

VMData *data;
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
Expand All @@ -513,8 +514,13 @@ static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
r_import_string);
VALUE r_import_name = rb_ary_entry(r_import_settings, 0);
char *import_name = StringValueCStr(r_import_name);
VALUE r_globalize = rb_ary_entry(r_import_settings, 1);
char *globalize = StringValueCStr(r_globalize);
VALUE r_default_exposure = rb_ary_entry(r_import_settings, 1);
char *globalize;
if (RTEST(r_custom_exposure)) {
globalize = StringValueCStr(r_custom_exposure);
} else {
globalize = StringValueCStr(r_default_exposure);
}

const char *importAndGlobalizeModule = "import %s from '%s';\n"
"%s\n";
Expand Down
7 changes: 7 additions & 0 deletions test/quickjs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ class Import < QuickjsVmTest

assert_equal(@vm.eval_code("Imported()"), "I am a default export of ESM.")
end

test "code_to_expose can differentiate the way to globalize" do
@vm.import('Imported', from: File.read('./test/fixture.esm.js'), code_to_expose: 'globalThis.RenamedImported = Imported;')

assert_equal(@vm.eval_code('RenamedImported()'), 'I am a default export of ESM.')
assert_equal(@vm.eval_code('!!globalThis.Imported'), false)
end
end

class ConsoleLoggers < QuickjsVmTest
Expand Down

0 comments on commit 3754c68

Please sign in to comment.