mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
refactor: 收敛 builtin 绑定表驱动并补完数组宏化收尾
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, configure_native_constructor};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -10,29 +10,29 @@ pub fn bind_boolean(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor = unsafe { &mut *ctor_ptr };
|
||||
let proto_ptr = kernel.builtin_world().boolean_proto.as_ptr() as *mut JsObject;
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
ctor.set_native_fn(Some(
|
||||
configure_native_constructor(
|
||||
ctor,
|
||||
crate::builtins::boolean::boolean_constructor as *const (),
|
||||
));
|
||||
ctor.set_native_arg_count(1);
|
||||
1,
|
||||
);
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
(
|
||||
"valueOf",
|
||||
crate::builtins::boolean::boolean_prototype_value_of,
|
||||
0
|
||||
),
|
||||
(
|
||||
"toString",
|
||||
crate::builtins::boolean::boolean_prototype_to_string,
|
||||
0
|
||||
),
|
||||
kernel,
|
||||
&[
|
||||
(
|
||||
"valueOf",
|
||||
crate::builtins::boolean::boolean_prototype_value_of as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toString",
|
||||
crate::builtins::boolean::boolean_prototype_to_string as *const (),
|
||||
0,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, bind_global_value, configure_native_constructor};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
@@ -10,102 +10,196 @@ pub fn bind_date(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor = unsafe { &mut *ctor_ptr };
|
||||
let proto_ptr = kernel.builtin_world().date_proto.as_ptr() as *mut JsObject;
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
ctor.set_native_fn(Some(crate::builtins::date::date_constructor as *const ()));
|
||||
ctor.set_native_arg_count(7);
|
||||
configure_native_constructor(
|
||||
ctor,
|
||||
crate::builtins::date::date_constructor as *const (),
|
||||
7,
|
||||
);
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
ctor,
|
||||
sf,
|
||||
sh,
|
||||
("now", crate::builtins::date::date_now, 0),
|
||||
("parse", crate::builtins::date::date_parse, 1),
|
||||
("UTC", crate::builtins::date::date_utc, 7),
|
||||
kernel,
|
||||
&[
|
||||
("now", crate::builtins::date::date_now as *const (), 0),
|
||||
("parse", crate::builtins::date::date_parse as *const (), 1),
|
||||
("UTC", crate::builtins::date::date_utc as *const (), 7),
|
||||
],
|
||||
);
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
("getTime", crate::builtins::date::date_get_time, 0),
|
||||
("getFullYear", crate::builtins::date::date_get_full_year, 0),
|
||||
("getMonth", crate::builtins::date::date_get_month, 0),
|
||||
("getDate", crate::builtins::date::date_get_date, 0),
|
||||
("getDay", crate::builtins::date::date_get_day, 0),
|
||||
("getHours", crate::builtins::date::date_get_hours, 0),
|
||||
("getMinutes", crate::builtins::date::date_get_minutes, 0),
|
||||
("getSeconds", crate::builtins::date::date_get_seconds, 0),
|
||||
(
|
||||
"getMilliseconds",
|
||||
crate::builtins::date::date_get_milliseconds,
|
||||
0
|
||||
),
|
||||
(
|
||||
"getUTCFullYear",
|
||||
crate::builtins::date::date_get_utc_full_year,
|
||||
0
|
||||
),
|
||||
("getUTCMonth", crate::builtins::date::date_get_utc_month, 0),
|
||||
("getUTCDate", crate::builtins::date::date_get_utc_date, 0),
|
||||
("getUTCDay", crate::builtins::date::date_get_utc_day, 0),
|
||||
("getUTCHours", crate::builtins::date::date_get_utc_hours, 0),
|
||||
(
|
||||
"getUTCMinutes",
|
||||
crate::builtins::date::date_get_utc_minutes,
|
||||
0
|
||||
),
|
||||
(
|
||||
"getUTCSeconds",
|
||||
crate::builtins::date::date_get_utc_seconds,
|
||||
0
|
||||
),
|
||||
(
|
||||
"getUTCMilliseconds",
|
||||
crate::builtins::date::date_get_utc_milliseconds,
|
||||
0
|
||||
),
|
||||
(
|
||||
"getTimezoneOffset",
|
||||
crate::builtins::date::date_get_timezone_offset,
|
||||
0
|
||||
),
|
||||
("setTime", crate::builtins::date::date_set_time, 1),
|
||||
("setFullYear", crate::builtins::date::date_set_full_year, 3),
|
||||
("setMonth", crate::builtins::date::date_set_month, 2),
|
||||
("setDate", crate::builtins::date::date_set_date, 1),
|
||||
("setHours", crate::builtins::date::date_set_hours, 4),
|
||||
("setMinutes", crate::builtins::date::date_set_minutes, 3),
|
||||
("setSeconds", crate::builtins::date::date_set_seconds, 2),
|
||||
(
|
||||
"setMilliseconds",
|
||||
crate::builtins::date::date_set_milliseconds,
|
||||
1
|
||||
),
|
||||
("toISOString", crate::builtins::date::date_to_iso_string, 0),
|
||||
("toJSON", crate::builtins::date::date_to_json, 1),
|
||||
("toString", crate::builtins::date::date_to_string, 0),
|
||||
(
|
||||
"toDateString",
|
||||
crate::builtins::date::date_to_date_string,
|
||||
0
|
||||
),
|
||||
(
|
||||
"toTimeString",
|
||||
crate::builtins::date::date_to_time_string,
|
||||
0
|
||||
),
|
||||
("toUTCString", crate::builtins::date::date_to_utc_string, 0),
|
||||
("valueOf", crate::builtins::date::date_value_of, 0),
|
||||
kernel,
|
||||
&[
|
||||
(
|
||||
"getTime",
|
||||
crate::builtins::date::date_get_time as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getFullYear",
|
||||
crate::builtins::date::date_get_full_year as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getMonth",
|
||||
crate::builtins::date::date_get_month as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getDate",
|
||||
crate::builtins::date::date_get_date as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getDay",
|
||||
crate::builtins::date::date_get_day as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getHours",
|
||||
crate::builtins::date::date_get_hours as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getMinutes",
|
||||
crate::builtins::date::date_get_minutes as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getSeconds",
|
||||
crate::builtins::date::date_get_seconds as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getMilliseconds",
|
||||
crate::builtins::date::date_get_milliseconds as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCFullYear",
|
||||
crate::builtins::date::date_get_utc_full_year as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCMonth",
|
||||
crate::builtins::date::date_get_utc_month as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCDate",
|
||||
crate::builtins::date::date_get_utc_date as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCDay",
|
||||
crate::builtins::date::date_get_utc_day as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCHours",
|
||||
crate::builtins::date::date_get_utc_hours as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCMinutes",
|
||||
crate::builtins::date::date_get_utc_minutes as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCSeconds",
|
||||
crate::builtins::date::date_get_utc_seconds as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getUTCMilliseconds",
|
||||
crate::builtins::date::date_get_utc_milliseconds as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"getTimezoneOffset",
|
||||
crate::builtins::date::date_get_timezone_offset as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"setTime",
|
||||
crate::builtins::date::date_set_time as *const (),
|
||||
1,
|
||||
),
|
||||
(
|
||||
"setFullYear",
|
||||
crate::builtins::date::date_set_full_year as *const (),
|
||||
3,
|
||||
),
|
||||
(
|
||||
"setMonth",
|
||||
crate::builtins::date::date_set_month as *const (),
|
||||
2,
|
||||
),
|
||||
(
|
||||
"setDate",
|
||||
crate::builtins::date::date_set_date as *const (),
|
||||
1,
|
||||
),
|
||||
(
|
||||
"setHours",
|
||||
crate::builtins::date::date_set_hours as *const (),
|
||||
4,
|
||||
),
|
||||
(
|
||||
"setMinutes",
|
||||
crate::builtins::date::date_set_minutes as *const (),
|
||||
3,
|
||||
),
|
||||
(
|
||||
"setSeconds",
|
||||
crate::builtins::date::date_set_seconds as *const (),
|
||||
2,
|
||||
),
|
||||
(
|
||||
"setMilliseconds",
|
||||
crate::builtins::date::date_set_milliseconds as *const (),
|
||||
1,
|
||||
),
|
||||
(
|
||||
"toISOString",
|
||||
crate::builtins::date::date_to_iso_string as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toJSON",
|
||||
crate::builtins::date::date_to_json as *const (),
|
||||
1,
|
||||
),
|
||||
(
|
||||
"toString",
|
||||
crate::builtins::date::date_to_string as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toDateString",
|
||||
crate::builtins::date::date_to_date_string as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toTimeString",
|
||||
crate::builtins::date::date_to_time_string as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"toUTCString",
|
||||
crate::builtins::date::date_to_utc_string as *const (),
|
||||
0,
|
||||
),
|
||||
(
|
||||
"valueOf",
|
||||
crate::builtins::date::date_value_of as *const (),
|
||||
0,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
let si_d = kernel.string_forge().intern("Date").0;
|
||||
let d_shape = kernel.shape_forge().make_shape(global.shape_id(), si_d);
|
||||
let d_val = JsValue::from_js_object(ctor_ptr);
|
||||
global.set_shape_id(d_shape);
|
||||
global.ensure_hash_props().push(Box::new(d_val));
|
||||
global.bump_generation();
|
||||
bind_global_value(kernel, global, "Date", JsValue::from_js_object(ctor_ptr));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, bind_global_value};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
@@ -9,20 +9,24 @@ pub fn bind_json(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let json_ptr = kernel.builtin_world().json_object.as_ptr() as *mut JsObject;
|
||||
let json = unsafe { &mut *json_ptr };
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
json,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
("parse", crate::builtins::json::json_parse, 1),
|
||||
("stringify", crate::builtins::json::json_stringify, 1),
|
||||
kernel,
|
||||
&[
|
||||
("parse", crate::builtins::json::json_parse as *const (), 1),
|
||||
(
|
||||
"stringify",
|
||||
crate::builtins::json::json_stringify as *const (),
|
||||
1,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
let si_j = kernel.string_forge().intern("JSON").0;
|
||||
let j_shape = kernel.shape_forge().make_shape(global.shape_id(), si_j);
|
||||
let j_val =
|
||||
JsValue::from_js_object(kernel.builtin_world().json_object.as_ptr() as *mut JsObject);
|
||||
global.set_shape_id(j_shape);
|
||||
global.ensure_hash_props().push(Box::new(j_val));
|
||||
global.bump_generation();
|
||||
bind_global_value(
|
||||
kernel,
|
||||
global,
|
||||
"JSON",
|
||||
JsValue::from_js_object(kernel.builtin_world().json_object.as_ptr() as *mut JsObject),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, configure_native_constructor};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -10,23 +10,21 @@ pub fn bind_map(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor = unsafe { &mut *ctor_ptr };
|
||||
let proto_ptr = kernel.builtin_world().map_proto.as_ptr() as *mut JsObject;
|
||||
|
||||
ctor.set_native_fn(Some(crate::builtins::map::map_constructor as *const ()));
|
||||
ctor.set_native_arg_count(1);
|
||||
configure_native_constructor(ctor, crate::builtins::map::map_constructor as *const (), 1);
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
("set", crate::builtins::map::map_set, 2),
|
||||
("get", crate::builtins::map::map_get, 1),
|
||||
("has", crate::builtins::map::map_has, 1),
|
||||
("delete", crate::builtins::map::map_delete, 1),
|
||||
("clear", crate::builtins::map::map_clear, 0),
|
||||
("size", crate::builtins::map::map_size, 0),
|
||||
kernel,
|
||||
&[
|
||||
("set", crate::builtins::map::map_set as *const (), 2),
|
||||
("get", crate::builtins::map::map_get as *const (), 1),
|
||||
("has", crate::builtins::map::map_has as *const (), 1),
|
||||
("delete", crate::builtins::map::map_delete as *const (), 1),
|
||||
("clear", crate::builtins::map::map_clear as *const (), 0),
|
||||
("size", crate::builtins::map::map_size as *const (), 0),
|
||||
],
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, configure_native_constructor};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -11,22 +11,26 @@ pub fn bind_regexp(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor = unsafe { &mut *ctor_ptr };
|
||||
let proto_ptr = kernel.builtin_world().regexp_proto.as_ptr() as *mut JsObject;
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
ctor.set_native_fn(Some(
|
||||
configure_native_constructor(
|
||||
ctor,
|
||||
crate::builtins::regexp::regexp_constructor as *const (),
|
||||
));
|
||||
ctor.set_native_arg_count(2);
|
||||
2,
|
||||
);
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
("exec", crate::builtins::regexp::regexp_exec, 1),
|
||||
("test", crate::builtins::regexp::regexp_test, 1),
|
||||
("toString", crate::builtins::regexp::regexp_to_string, 0),
|
||||
kernel,
|
||||
&[
|
||||
("exec", crate::builtins::regexp::regexp_exec as *const (), 1),
|
||||
("test", crate::builtins::regexp::regexp_test as *const (), 1),
|
||||
(
|
||||
"toString",
|
||||
crate::builtins::regexp::regexp_to_string as *const (),
|
||||
0,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, configure_native_constructor};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
@@ -10,22 +10,20 @@ pub fn bind_set(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor = unsafe { &mut *ctor_ptr };
|
||||
let proto_ptr = kernel.builtin_world().set_proto.as_ptr() as *mut JsObject;
|
||||
|
||||
ctor.set_native_fn(Some(crate::builtins::set::set_constructor as *const ()));
|
||||
ctor.set_native_arg_count(1);
|
||||
configure_native_constructor(ctor, crate::builtins::set::set_constructor as *const (), 1);
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
bind_methods!(
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
("add", crate::builtins::set::set_add, 1),
|
||||
("has", crate::builtins::set::set_has, 1),
|
||||
("delete", crate::builtins::set::set_delete, 1),
|
||||
("clear", crate::builtins::set::set_clear, 0),
|
||||
("size", crate::builtins::set::set_size, 0),
|
||||
kernel,
|
||||
&[
|
||||
("add", crate::builtins::set::set_add as *const (), 1),
|
||||
("has", crate::builtins::set::set_has as *const (), 1),
|
||||
("delete", crate::builtins::set::set_delete as *const (), 1),
|
||||
("clear", crate::builtins::set::set_clear as *const (), 0),
|
||||
("size", crate::builtins::set::set_size as *const (), 0),
|
||||
],
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_methods;
|
||||
use crate::bindings::{apply_binding_table, configure_native_constructor};
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
@@ -12,15 +12,22 @@ pub fn bind_symbol(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor = unsafe { &mut *ctor_ptr };
|
||||
let proto_ptr = kernel.builtin_world().symbol_proto.as_ptr() as *mut JsObject;
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
bind_methods!(
|
||||
configure_native_constructor(
|
||||
ctor,
|
||||
crate::builtins::symbol::symbol_constructor as *const (),
|
||||
1,
|
||||
);
|
||||
|
||||
apply_binding_table(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
("toString", crate::builtins::symbol::symbol_to_string, 0),
|
||||
kernel,
|
||||
&[(
|
||||
"toString",
|
||||
crate::builtins::symbol::symbol_to_string as *const (),
|
||||
0,
|
||||
)],
|
||||
);
|
||||
|
||||
for (name, val) in [
|
||||
@@ -68,4 +75,5 @@ fn bind_well_known_symbol(
|
||||
let shape_id = kernel.shape_forge().make_shape(ctor.shape_id(), si);
|
||||
ctor.set_shape_id(shape_id);
|
||||
ctor.ensure_hash_props().push(Box::new(val));
|
||||
ctor.bump_generation();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@ pub mod bind_symbol;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::builtin::BuiltinWorld;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! bind_constructor {
|
||||
@@ -46,6 +49,41 @@ macro_rules! bind_constructor_hash {
|
||||
}};
|
||||
}
|
||||
|
||||
pub(crate) fn configure_native_constructor(
|
||||
ctor: &mut JsObject,
|
||||
native_fn: *const (),
|
||||
arg_count: u8,
|
||||
) {
|
||||
ctor.set_native_fn(Some(native_fn));
|
||||
ctor.set_native_arg_count(arg_count);
|
||||
}
|
||||
|
||||
pub(crate) fn apply_binding_table(
|
||||
world: &BuiltinWorld,
|
||||
target: &mut JsObject,
|
||||
kernel: &Arc<OxideKernel>,
|
||||
bindings: &[(&'static str, *const (), u8)],
|
||||
) {
|
||||
let shape_forge = kernel.shape_forge().as_ref();
|
||||
let string_forge = kernel.string_forge().as_ref();
|
||||
for (name, func, nargs) in bindings {
|
||||
let _ = world.bind_method(target, shape_forge, string_forge, name, *func, *nargs);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn bind_global_value(
|
||||
kernel: &Arc<OxideKernel>,
|
||||
global: &mut JsObject,
|
||||
name: &str,
|
||||
value: JsValue,
|
||||
) {
|
||||
let si = kernel.string_forge().intern(name).0;
|
||||
let shape = kernel.shape_forge().make_shape(global.shape_id(), si);
|
||||
global.set_shape_id(shape);
|
||||
global.ensure_hash_props().push(Box::new(value));
|
||||
global.bump_generation();
|
||||
}
|
||||
|
||||
pub fn init_kernel_builtins(kernel: &Arc<OxideKernel>) {
|
||||
let global_ptr = kernel.global_object().as_ptr() as *mut oxide_types::object::JsObject;
|
||||
let global = unsafe { &mut *global_ptr };
|
||||
|
||||
@@ -6,6 +6,20 @@ use crate::coercion;
|
||||
use crate::native::{NativeFn, NativeResult};
|
||||
use crate::vm::Vm;
|
||||
|
||||
macro_rules! array_ptr {
|
||||
($vm:expr, $args:expr) => {
|
||||
get_this_array_ref($vm.reg($args[0]))?
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! array_ptr_len {
|
||||
($vm:expr, $args:expr) => {{
|
||||
let arr_ptr = array_ptr!($vm, $args);
|
||||
let len = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
(arr_ptr, len)
|
||||
}};
|
||||
}
|
||||
|
||||
fn get_this_array_ref(val: JsValue) -> Result<*mut JsObject, JsValue> {
|
||||
if !val.is_object() {
|
||||
return Err(JsValue::undefined());
|
||||
@@ -110,7 +124,7 @@ pub fn array_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_push(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let mut len = arr.prop_count();
|
||||
for &arg_reg in args.iter().skip(1) {
|
||||
@@ -123,7 +137,7 @@ pub fn array_push(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_pop(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let len = arr.prop_count();
|
||||
if len == 0 {
|
||||
@@ -135,7 +149,7 @@ pub fn array_pop(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_slice(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
let start = if args.len() > 1 {
|
||||
@@ -163,7 +177,7 @@ pub fn array_slice(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_splice(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
|
||||
@@ -225,7 +239,7 @@ pub fn array_splice(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_concat(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
let mut all: Vec<JsValue> = Vec::new();
|
||||
@@ -260,7 +274,7 @@ pub fn array_concat(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_join(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
let sep = if args.len() > 1 {
|
||||
@@ -285,7 +299,7 @@ pub fn array_join(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_index_of(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
if args.len() < 2 {
|
||||
@@ -301,7 +315,7 @@ pub fn array_index_of(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_includes(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
if args.len() < 2 {
|
||||
@@ -325,7 +339,7 @@ pub fn array_includes(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_reverse(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
let mut i = 0;
|
||||
@@ -341,7 +355,7 @@ pub fn array_reverse(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_flat(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let n = arr.prop_count() as usize;
|
||||
let depth = if args.len() > 1 {
|
||||
@@ -385,8 +399,7 @@ pub fn array_flat(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_for_each(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Err(JsValue::undefined());
|
||||
}
|
||||
@@ -412,8 +425,7 @@ pub fn array_for_each(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Err(JsValue::undefined());
|
||||
}
|
||||
@@ -448,8 +460,7 @@ pub fn array_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_filter(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Err(JsValue::undefined());
|
||||
}
|
||||
@@ -491,8 +502,7 @@ pub fn array_filter(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_reduce(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if n == 0 && args.len() < 3 {
|
||||
return Err(JsValue::undefined());
|
||||
}
|
||||
@@ -530,8 +540,7 @@ pub fn array_reduce(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_find(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::undefined());
|
||||
}
|
||||
@@ -565,8 +574,7 @@ pub fn array_find(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_some(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::bool(false));
|
||||
}
|
||||
@@ -600,8 +608,7 @@ pub fn array_some(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_every(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::bool(false));
|
||||
}
|
||||
@@ -635,8 +642,7 @@ pub fn array_every(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_flat_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Err(JsValue::undefined());
|
||||
}
|
||||
@@ -688,7 +694,7 @@ pub fn array_flat_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_shift(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let len = arr.prop_count();
|
||||
if len == 0 {
|
||||
@@ -704,7 +710,7 @@ pub fn array_shift(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_unshift(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let len = arr.prop_count();
|
||||
let n_items = args.len().saturating_sub(1);
|
||||
@@ -721,7 +727,7 @@ pub fn array_unshift(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_fill(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let len = arr.prop_count() as usize;
|
||||
let value = if args.len() > 1 {
|
||||
@@ -748,7 +754,7 @@ pub fn array_fill(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_copy_within(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let len = arr.prop_count() as usize;
|
||||
if len == 0 {
|
||||
@@ -783,7 +789,7 @@ pub fn array_copy_within(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_at(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let len = arr.prop_count() as i32;
|
||||
let mut index = if args.len() > 1 {
|
||||
@@ -801,7 +807,7 @@ pub fn array_at(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_last_index_of(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, args);
|
||||
let arr = unsafe { &*arr_ptr };
|
||||
let len = arr.prop_count() as i32;
|
||||
let search = if args.len() > 1 {
|
||||
@@ -818,8 +824,7 @@ pub fn array_last_index_of(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_find_index(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::int(-1));
|
||||
}
|
||||
@@ -849,8 +854,10 @@ pub fn array_find_index(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_find_last(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as i32;
|
||||
let (arr_ptr, n) = {
|
||||
let (arr_ptr, len) = array_ptr_len!(vm, args);
|
||||
(arr_ptr, len as i32)
|
||||
};
|
||||
if args.len() < 2 {
|
||||
return Ok(JsValue::undefined());
|
||||
}
|
||||
@@ -880,8 +887,7 @@ pub fn array_find_last(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_reduce_right(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
|
||||
let n = unsafe { (*arr_ptr).prop_count() } as usize;
|
||||
let (arr_ptr, n) = array_ptr_len!(vm, args);
|
||||
if args.len() < 2 {
|
||||
return Err(JsValue::undefined());
|
||||
}
|
||||
@@ -910,7 +916,7 @@ pub fn array_reduce_right(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
||||
}
|
||||
|
||||
pub fn array_sort(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
||||
let arr_ptr = get_this_array_ref(vm.reg(_args[0]))?;
|
||||
let arr_ptr = array_ptr!(vm, _args);
|
||||
let arr = unsafe { &mut *arr_ptr };
|
||||
let len = arr.prop_count() as usize;
|
||||
let mut vals: Vec<JsValue> = (0..len).map(|i| arr.get_prop_at(i)).collect();
|
||||
|
||||
@@ -19,38 +19,8 @@ use oxide_types::value::JsValue;
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! throw_err {
|
||||
($self:ident, Error, $msg:expr) => {{
|
||||
let error = crate::builtins::error::create_error($self, $msg);
|
||||
$self.exception_value = Some(error);
|
||||
$self.pending_error_kind = Some("Error");
|
||||
match $self.unwind() {
|
||||
Ok(()) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}};
|
||||
($self:ident, TypeError, $msg:expr) => {{
|
||||
let error = crate::builtins::error::create_type_error($self, $msg);
|
||||
$self.exception_value = Some(error);
|
||||
$self.pending_error_kind = Some("TypeError");
|
||||
match $self.unwind() {
|
||||
Ok(()) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}};
|
||||
($self:ident, ReferenceError, $msg:expr) => {{
|
||||
let error = crate::builtins::error::create_reference_error($self, $msg);
|
||||
$self.exception_value = Some(error);
|
||||
$self.pending_error_kind = Some("ReferenceError");
|
||||
match $self.unwind() {
|
||||
Ok(()) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}};
|
||||
($self:ident, SyntaxError, $msg:expr) => {{
|
||||
let error = crate::builtins::error::create_syntax_error($self, $msg);
|
||||
$self.exception_value = Some(error);
|
||||
$self.pending_error_kind = Some("SyntaxError");
|
||||
match $self.unwind() {
|
||||
($self:ident, $kind:ident, $msg:expr) => {{
|
||||
match $self.raise_error_kind(stringify!($kind), $msg) {
|
||||
Ok(()) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user