Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions libs/jsruntime/src/builtins/builtin_mod.rs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,50 +32,55 @@ impl<X> Runtime<X> {
matches!(value, Value::Object(object) if self.is_{{metadata.id}}_object(*object))
}

/// Creates the {{metadata.class}} constructor.
pub(super) fn create_{{metadata.id}}_constructor(&mut self) -> HandleMut<Object> {
logger::debug!(event = "create_{{metadata.id}}_constructor");
/// Initialize the {{metadata.class}} constructor.
pub(super) fn init_{{metadata.id}}_constructor(&mut self) {
logger::debug!(event = "init_{{metadata.id}}_constructor");

#[allow(unused_mut)]
let mut constructor = self.create_builtin_function(&BuiltinFunctionParams {
lambda: constructor::<X>,
let mut constructor = self.builtins.{{metadata.id}}_constructor;

self.init_builtin_function(constructor, &BuiltinFunctionParams {
lambda: {{metadata.id}}_constructor::<X>,
name: const_string_handle!("{{metadata.class}}"),
length: 1,
slots: &[],
prototype: Some(self.builtins.{{metadata.id}}_prototype),
});

constructor.set_constructor();

let result = constructor.define_own_property(
Symbol::PROTOTYPE.into(),
Property::data_xxx(Value::Object(self.builtins.{{metadata.id}}_prototype)),
);
debug_assert!(matches!(result, Ok(true)));

{{#each constructorProperties}}
{{#if (eq kind "constructor.function")}}
let func = self.create_builtin_function(&BuiltinFunctionParams {
lambda: {{imp}},
name: const_string_handle!("{{name}}"),
length: {{this.length}},
slots: &[],
prototype: None,
});
let result = constructor.define_own_property(Symbol::{{symbol}}.into(), Property::data_wxc(Value::Object(func)));
let result = constructor.define_own_property(
Symbol::{{symbol}}.into(), Property::data_wxc(Value::Object(func)));
debug_assert!(matches!(result, Ok(true)));

{{/if}}
{{/each}}
// TODO(refactor): bind outside this function
let result = self.builtins.{{metadata.id}}_prototype.define_own_property(Symbol::CONSTRUCTOR.into(), Property::data_wxc(Value::Object(constructor)));
debug_assert!(matches!(result, Ok(true)));

constructor
}

/// Creates the {{metadata.class}} prototype object.
/// Initializes the {{metadata.class}} prototype object.
pub(super) fn init_{{metadata.id}}_prototype(&mut self) {
logger::debug!(event = "init_{{metadata.id}}_prototype");

#[allow(unused)]
let mut prototype = self.builtins.{{metadata.id}}_prototype;
{{#if metadata.inherits}}
prototype.set_prototype(self.builtins.{{metadata.inherits}}_prototype);
{{/if}}

let result = prototype.define_own_property(Symbol::CONSTRUCTOR.into(), Property::data_wxc(Value::Object(self.builtins.{{metadata.id}}_constructor)));
debug_assert!(matches!(result, Ok(true)));

{{#each prototypeProperties}}
{{#if (eq kind "prototype.property")}}
imp::{{imp}}(self, prototype);
Expand All @@ -86,7 +91,6 @@ impl<X> Runtime<X> {
name: const_string_handle!("{{name}}"),
length: {{this.length}},
slots: &[],
prototype: None,
});
let result = prototype.define_own_property(Symbol::{{symbol}}.into(), Property::data_wxc(Value::Object(func)));
debug_assert!(matches!(result, Ok(true)));
Expand All @@ -97,9 +101,8 @@ impl<X> Runtime<X> {
}

// lambda functions
// TODO: use proc-macro

extern "C" fn constructor<X>(
extern "C" fn {{metadata.id}}_constructor<X>(
runtime: &mut Runtime<X>,
context: &mut CallContext,
retv: &mut Value,
Expand Down
6 changes: 3 additions & 3 deletions libs/jsruntime/src/builtins/function/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn function_prototype_bind<X>(
let target = runtime.this_func(context.this())?;
let this = context.arg(0);
let args = &context.args()[1..];
let mut bound_func = runtime.bound_function_create(target, this, args)?;
let bound_func = runtime.bound_function_create(target, this, args)?;
let length = match target.get_own_property(&Symbol::LENGTH.into()) {
Some(prop) => {
let target_length = runtime.value_to_length(prop.value())?;
Expand All @@ -82,12 +82,12 @@ pub fn function_prototype_bind<X>(
None => 0,
};
debug_assert!(length <= u16::MAX as u64);
runtime.set_function_length(&mut bound_func, length as u16);
runtime.set_function_length(bound_func, length as u16);
let target_name = target
.get_value(&Symbol::NAME.into())
.unwrap_or(&Value::Undefined);
if let Value::String(name) = target_name {
runtime.set_function_name(&mut bound_func, *name);
runtime.set_function_name(bound_func, *name);
}
Ok(Value::Object(bound_func))
}
Expand Down
26 changes: 13 additions & 13 deletions libs/jsruntime/src/builtins/global/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,78 +89,78 @@ pub fn parse_int<X>(runtime: &mut Runtime<X>, context: &mut CallContext) -> Resu

//#sec-constructor-properties-of-the-global-object-aggregate-error global.constructor
pub fn define_aggregate_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_aggregate_error_constructor();
let constructor = runtime.builtins.aggregate_error_constructor;
runtime.define_constructor(Symbol::AGGREGATE_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-error global.constructor
pub fn define_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_error_constructor();
let constructor = runtime.builtins.error_constructor;
runtime.define_constructor(Symbol::ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-evalerror global.constructor
pub fn define_eval_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_eval_error_constructor();
let constructor = runtime.builtins.eval_error_constructor;
runtime.define_constructor(Symbol::EVAL_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-function global.constructor
pub fn define_function_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_function_constructor();
let constructor = runtime.builtins.function_constructor;
runtime.define_constructor(Symbol::FUNCTION, constructor);
}

//#_internalerror global.constructor { "name": "InternalError" }
pub fn define_internal_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_internal_error_constructor();
let constructor = runtime.builtins.internal_error_constructor;
runtime.define_constructor(Symbol::INTERNAL_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-object global.constructor
pub fn define_object_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_object_constructor();
let constructor = runtime.builtins.object_constructor;
runtime.define_constructor(Symbol::OBJECT, constructor);
}

//#sec-constructor-properties-of-the-global-object-promise global.constructor
pub fn define_promise_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_promise_constructor();
let constructor = runtime.builtins.promise_constructor;
runtime.define_constructor(Symbol::PROMISE, constructor);
}

//#sec-constructor-properties-of-the-global-object-rangeerror global.constructor
pub fn define_range_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_range_error_constructor();
let constructor = runtime.builtins.range_error_constructor;
runtime.define_constructor(Symbol::RANGE_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-referenceerror global.constructor
pub fn define_reference_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_reference_error_constructor();
let constructor = runtime.builtins.reference_error_constructor;
runtime.define_constructor(Symbol::REFERENCE_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-string global.constructor
pub fn define_string_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_string_constructor();
let constructor = runtime.builtins.string_constructor;
runtime.define_constructor(Symbol::STRING, constructor);
}

//#sec-constructor-properties-of-the-global-object-syntaxerror global.constructor
pub fn define_syntax_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_syntax_error_constructor();
let constructor = runtime.builtins.syntax_error_constructor;
runtime.define_constructor(Symbol::SYNTAX_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-typeerror global.constructor
pub fn define_type_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_type_error_constructor();
let constructor = runtime.builtins.type_error_constructor;
runtime.define_constructor(Symbol::TYPE_ERROR, constructor);
}

//#sec-constructor-properties-of-the-global-object-urierror global.constructor
pub fn define_uri_error_constructor<X>(runtime: &mut Runtime<X>) {
let constructor = runtime.create_uri_error_constructor();
let constructor = runtime.builtins.uri_error_constructor;
runtime.define_constructor(Symbol::URI_ERROR, constructor);
}
2 changes: 0 additions & 2 deletions libs/jsruntime/src/builtins/global/mod.rs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ impl<X> Runtime<X> {
name: const_string_handle!("{{name}}"),
length: {{this.length}},
slots: &[],
prototype: None,
});
self.define_global_property(Symbol::{{symbol}}, Property::data_wxc(Value::Object(func)));

Expand All @@ -57,7 +56,6 @@ impl<X> Runtime<X> {
}

// lambda functions
// TODO: use proc-macro

{{#each globalProperties}}
{{#if (eq kind "global.function")}}
Expand Down
Loading
Loading