Browse Source

Fix `Context::register_global_function()` (#1270)

pull/1278/head
Halid Odat 4 years ago committed by GitHub
parent
commit
a5e38133c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 55
      boa/src/context.rs

55
boa/src/context.rs

@ -8,7 +8,7 @@ use crate::{
}, },
class::{Class, ClassBuilder}, class::{Class, ClassBuilder},
exec::Interpreter, exec::Interpreter,
object::{GcObject, Object, PROTOTYPE}, object::{FunctionBuilder, GcObject, Object, PROTOTYPE},
property::{Attribute, DataDescriptor, PropertyKey}, property::{Attribute, DataDescriptor, PropertyKey},
realm::Realm, realm::Realm,
symbol::{RcSymbol, Symbol}, symbol::{RcSymbol, Symbol},
@ -503,34 +503,18 @@ impl Context {
Ok(val) Ok(val)
} }
/// Create a new builin function.
pub fn create_builtin_function(
&mut self,
name: &str,
length: usize,
body: NativeFunction,
) -> Result<GcObject> {
let function_prototype: Value = self.standard_objects().object_object().prototype().into();
// Every new function has a prototype property pre-made
let proto = Value::new_object(self);
let mut function = GcObject::new(Object::function(
Function::BuiltIn(body.into(), FunctionFlags::CALLABLE),
function_prototype,
));
function.set(PROTOTYPE.into(), proto, function.clone().into(), self)?;
function.set(
"length".into(),
length.into(),
function.clone().into(),
self,
)?;
function.set("name".into(), name.into(), function.clone().into(), self)?;
Ok(function)
}
/// Register a global function. /// Register a global function.
///
/// The function will be both `callable` and `constructable` (call with `new`).
///
/// The function will be bound to the global object with `writable`, `non-enumerable`
/// and `configurable` attributes. The same as when you create a function in JavaScript.
///
/// # Note
///
/// If you want to make a function only `callable` or `constructable`, or wish to bind it differently
/// to the global object, you can create the function object with [`FunctionBuilder`](crate::object::FunctionBuilder).
/// And bind it to the global object with [`Context::register_global_property`](Context::register_global_property) method.
#[inline] #[inline]
pub fn register_global_function( pub fn register_global_function(
&mut self, &mut self,
@ -538,9 +522,18 @@ impl Context {
length: usize, length: usize,
body: NativeFunction, body: NativeFunction,
) -> Result<()> { ) -> Result<()> {
let function = self.create_builtin_function(name, length, body)?; let function = FunctionBuilder::new(self, body)
let mut global = self.global_object(); .name(name)
global.insert_property(name, function, Attribute::all()); .length(length)
.callable(true)
.constructable(true)
.build();
self.global_object().insert_property(
name,
function,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
);
Ok(()) Ok(())
} }

Loading…
Cancel
Save