Browse Source

Add Example to Execute a Function of a Script File (#1357)

Co-authored-by: Halid Odat <halidodat@gmail.com>
pull/1424/head
Marc Schreiber 3 years ago committed by GitHub
parent
commit
970a611d28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      boa/src/context.rs

32
boa/src/context.rs

@ -217,6 +217,38 @@ impl StandardObjects {
/// `Context`s constructed in a thread share the same runtime, therefore it
/// is possible to share objects from one context to another context, but they
/// have to be in the same thread.
///
/// # Examples
///
/// ## Execute Function of Script File
///
/// ```rust
/// use boa::{Context, object::ObjectInitializer, property::Attribute};
///
/// let script = r#"
/// function test(arg1) {
/// if(arg1 != null) {
/// return arg1.x;
/// }
/// return 112233;
/// }
/// "#;
///
/// let mut context = Context::new();
///
/// // Populate the script definition to the context.
/// context.eval(script).unwrap();
///
/// // Create an object that can be used in eval calls.
/// let arg = ObjectInitializer::new(&mut context)
/// .property("x", 12, Attribute::READONLY)
/// .build();
/// context.register_global_property("arg", arg, Attribute::all());
///
/// let value = context.eval("test(arg)").unwrap();
///
/// assert_eq!(value.as_number(), Some(12.0))
/// ```
#[derive(Debug)]
pub struct Context {
/// realm holds both the global object and the environment

Loading…
Cancel
Save