How can I override the Math.random() standard function? #3698
Answered
by
jedel1043
fabiopolimeni
asked this question in
Q&A
-
Hi, I am seeking for a solution to replace the Math.random() with a custom solution. If I were in JS land, I could simply do something like this Math.random = () => {return 1;} So, what would be a good strategy to override that with Boa? Either rust or js solutions would be fine. |
Beta Was this translation helpful? Give feedback.
Answered by
jedel1043
Feb 27, 2024
Replies: 1 comment 1 reply
-
All builtin objects should be overridable from the context's intrinsics: let context = &mut Context::default();
let math = context.intrinsics().objects().math();
math.set(
js_string!("random"),
NativeFunction::from_fn_ptr(|_, _, _| Ok(JsValue::from(1))).to_js_function(context.realm()),
true,
context,
)
.unwrap();
assert_eq!(
context.eval(Source::from_bytes("Math.random()")),
Ok(JsValue::Integer(1))
);
assert_eq!(
context.eval(Source::from_bytes("Math.random()")),
Ok(JsValue::Integer(1))
); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fabiopolimeni
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All builtin objects should be overridable from the context's intrinsics: