Skip to content

Commit

Permalink
stripping unnecessary globals could be done by stopping js_std_add_he…
Browse files Browse the repository at this point in the history
…lpers
  • Loading branch information
hmsk committed Jul 30, 2024
1 parent 32f99c1 commit d5a5fcb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 deletions.
7 changes: 0 additions & 7 deletions ext/quickjsrb/quickjsrb.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,17 +372,10 @@ static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
JS_AddIntrinsicBigDecimal(data->context);
JS_AddIntrinsicOperators(data->context);
JS_EnableBignumExt(data->context, TRUE);
js_std_add_helpers(data->context, 0, NULL);

JS_SetModuleLoaderFunc(runtime, NULL, js_module_loader, NULL);
js_std_init_handlers(runtime);

const char *stripGlobals = "delete(globalThis.scriptArgs);\n"
"delete(globalThis.console);\n"
"delete(print);\n";
JSValue j_stripGlobals = JS_Eval(data->context, stripGlobals, strlen(stripGlobals), "<vm>", JS_EVAL_TYPE_GLOBAL);
JS_FreeValue(data->context, j_stripGlobals);

if (RTEST(rb_funcall(r_features, rb_intern("include?"), 1, ID2SYM(rb_intern(featureStdId)))))
{
js_init_module_std(data->context, "std");
Expand Down
3 changes: 2 additions & 1 deletion test/quickjs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class WithPlainVM < QuickjsVmTest
assert_equal(@vm.eval_code("typeof os === 'undefined'"), true)
end

test "does not have default globals by Quickjs" do
test "does not have std helpers" do
assert_equal(@vm.eval_code("typeof __loadScript === 'undefined'"), true)
assert_equal(@vm.eval_code("typeof scriptArgs === 'undefined'"), true)
assert_equal(@vm.eval_code("typeof console === 'undefined'"), true)
assert_equal(@vm.eval_code("typeof print === 'undefined'"), true)
Expand Down

0 comments on commit d5a5fcb

Please sign in to comment.