Skip to content

Commit 9c13974

Browse files
committed
Runtime: improve eval functions
- Use indirect eval (faster and avoid variable captures) - Put expressions within parentheses to allow arbitrary expressions without confusion with statements. Add a `"use strict"` directive since this is not inherited from the enclosing function with indirect eval.
1 parent 27c593b commit 9c13974

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

runtime/js/jslib.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function caml_js_var(x) {
280280
//console.error("Js.Unsafe.eval_string")
281281
}
282282
// biome-ignore lint/security/noGlobalEval:
283-
return eval(x);
283+
return eval?.(x);
284284
}
285285
//Provides: caml_js_call (const, mutable, shallow)
286286
//Requires: caml_js_from_array
@@ -479,23 +479,23 @@ function caml_js_strict_equals(x, y) {
479479
//Requires: caml_jsstring_of_string
480480
function caml_js_eval_string(s) {
481481
// biome-ignore lint/security/noGlobalEval:
482-
return eval(caml_jsstring_of_string(s));
482+
return eval?.('"use strict";' + caml_jsstring_of_string(s));
483483
}
484484

485485
//Provides: caml_js_expr (const)
486486
//Requires: caml_jsstring_of_string
487487
function caml_js_expr(s) {
488488
console.error("caml_js_expr: fallback to runtime evaluation\n");
489489
// biome-ignore lint/security/noGlobalEval:
490-
return eval(caml_jsstring_of_string(s));
490+
return eval?.('"use strict";(' + caml_jsstring_of_string(s) + ")");
491491
}
492492

493493
//Provides: caml_pure_js_expr const (const)
494494
//Requires: caml_jsstring_of_string
495495
function caml_pure_js_expr(s) {
496496
console.error("caml_pure_js_expr: fallback to runtime evaluation\n");
497497
// biome-ignore lint/security/noGlobalEval:
498-
return eval(caml_jsstring_of_string(s));
498+
return eval?.('"use strict";(' + caml_jsstring_of_string(s) + ")");
499499
}
500500

501501
//Provides: caml_js_object (object_literal)

0 commit comments

Comments
 (0)