Skip to content

How can I override the Math.random() standard function? #3698

Answered by jedel1043
fabiopolimeni asked this question in Q&A
Discussion options

You must be logged in to vote

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))
);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@fabiopolimeni
Comment options

Answer selected by fabiopolimeni
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants