refactor: 拆分绑定函数与封装宏以简化模块实现
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::builtin::ArrayMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
pub fn bind_array(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let _array_methods = ArrayMethods {
|
||||
push: crate::builtins::array::array_push as *const (),
|
||||
pop: crate::builtins::array::array_pop as *const (),
|
||||
slice: crate::builtins::array::array_slice as *const (),
|
||||
splice: crate::builtins::array::array_splice as *const (),
|
||||
concat: crate::builtins::array::array_concat as *const (),
|
||||
join: crate::builtins::array::array_join as *const (),
|
||||
index_of: crate::builtins::array::array_index_of as *const (),
|
||||
includes: crate::builtins::array::array_includes as *const (),
|
||||
reverse: crate::builtins::array::array_reverse as *const (),
|
||||
for_each: crate::builtins::array::array_for_each as *const (),
|
||||
map: crate::builtins::array::array_map as *const (),
|
||||
filter: crate::builtins::array::array_filter as *const (),
|
||||
reduce: crate::builtins::array::array_reduce as *const (),
|
||||
find: crate::builtins::array::array_find as *const (),
|
||||
some: crate::builtins::array::array_some as *const (),
|
||||
every: crate::builtins::array::array_every as *const (),
|
||||
flat: crate::builtins::array::array_flat as *const (),
|
||||
flat_map: crate::builtins::array::array_flat_map as *const (),
|
||||
};
|
||||
|
||||
kernel.builtin_world().bind_array_methods(
|
||||
&_array_methods,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
|
||||
let ctor_ptr = kernel.builtin_world().array_constructor.as_ptr() as *mut JsObject;
|
||||
bind_constructor_hash!(
|
||||
kernel,
|
||||
global,
|
||||
"Array",
|
||||
ctor_ptr,
|
||||
crate::builtins::array::array_constructor,
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::bind_method;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
pub fn bind_boolean(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor_ptr = kernel.builtin_world().boolean_constructor.as_ptr() as *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(
|
||||
crate::builtins::boolean::boolean_constructor as *const (),
|
||||
));
|
||||
ctor.set_native_arg_count(1);
|
||||
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"valueOf",
|
||||
crate::builtins::boolean::boolean_prototype_value_of,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toString",
|
||||
crate::builtins::boolean::boolean_prototype_to_string,
|
||||
0
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
kernel,
|
||||
global,
|
||||
"Boolean",
|
||||
ctor_ptr,
|
||||
crate::builtins::boolean::boolean_constructor,
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_method;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
pub fn bind_date(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor_ptr = kernel.builtin_world().date_constructor.as_ptr() as *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);
|
||||
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
ctor,
|
||||
sf,
|
||||
sh,
|
||||
"now",
|
||||
crate::builtins::date::date_now,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
ctor,
|
||||
sf,
|
||||
sh,
|
||||
"parse",
|
||||
crate::builtins::date::date_parse,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
ctor,
|
||||
sf,
|
||||
sh,
|
||||
"UTC",
|
||||
crate::builtins::date::date_utc,
|
||||
7
|
||||
);
|
||||
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getTime",
|
||||
crate::builtins::date::date_get_time,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getFullYear",
|
||||
crate::builtins::date::date_get_full_year,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getMonth",
|
||||
crate::builtins::date::date_get_month,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getDate",
|
||||
crate::builtins::date::date_get_date,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getDay",
|
||||
crate::builtins::date::date_get_day,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getHours",
|
||||
crate::builtins::date::date_get_hours,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getMinutes",
|
||||
crate::builtins::date::date_get_minutes,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getSeconds",
|
||||
crate::builtins::date::date_get_seconds,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getMilliseconds",
|
||||
crate::builtins::date::date_get_milliseconds,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCFullYear",
|
||||
crate::builtins::date::date_get_utc_full_year,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCMonth",
|
||||
crate::builtins::date::date_get_utc_month,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCDate",
|
||||
crate::builtins::date::date_get_utc_date,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCDay",
|
||||
crate::builtins::date::date_get_utc_day,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCHours",
|
||||
crate::builtins::date::date_get_utc_hours,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCMinutes",
|
||||
crate::builtins::date::date_get_utc_minutes,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCSeconds",
|
||||
crate::builtins::date::date_get_utc_seconds,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getUTCMilliseconds",
|
||||
crate::builtins::date::date_get_utc_milliseconds,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"getTimezoneOffset",
|
||||
crate::builtins::date::date_get_timezone_offset,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setTime",
|
||||
crate::builtins::date::date_set_time,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setFullYear",
|
||||
crate::builtins::date::date_set_full_year,
|
||||
3
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setMonth",
|
||||
crate::builtins::date::date_set_month,
|
||||
2
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setDate",
|
||||
crate::builtins::date::date_set_date,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setHours",
|
||||
crate::builtins::date::date_set_hours,
|
||||
4
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setMinutes",
|
||||
crate::builtins::date::date_set_minutes,
|
||||
3
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setSeconds",
|
||||
crate::builtins::date::date_set_seconds,
|
||||
2
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"setMilliseconds",
|
||||
crate::builtins::date::date_set_milliseconds,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toISOString",
|
||||
crate::builtins::date::date_to_iso_string,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toJSON",
|
||||
crate::builtins::date::date_to_json,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toString",
|
||||
crate::builtins::date::date_to_string,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toDateString",
|
||||
crate::builtins::date::date_to_date_string,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toTimeString",
|
||||
crate::builtins::date::date_to_time_string,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"toUTCString",
|
||||
crate::builtins::date::date_to_utc_string,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"valueOf",
|
||||
crate::builtins::date::date_value_of,
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::builtin::ErrorMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
pub fn bind_error(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let error_methods = ErrorMethods {
|
||||
error: crate::builtins::error::error_constructor as *const (),
|
||||
type_error: crate::builtins::error::type_error_constructor as *const (),
|
||||
reference_error: crate::builtins::error::reference_error_constructor as *const (),
|
||||
range_error: crate::builtins::error::range_error_constructor as *const (),
|
||||
syntax_error: crate::builtins::error::syntax_error_constructor as *const (),
|
||||
uri_error: crate::builtins::error::uri_error_constructor as *const (),
|
||||
eval_error: crate::builtins::error::eval_error_constructor as *const (),
|
||||
};
|
||||
kernel.builtin_world().bind_error_methods(
|
||||
&error_methods,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
|
||||
let si_err = kernel.string_forge().intern("Error").0;
|
||||
let err_shape = kernel.shape_forge().make_shape(global.shape_id(), si_err);
|
||||
let err_val =
|
||||
JsValue::from_js_object(kernel.builtin_world().error_constructor.as_ptr() as *mut JsObject);
|
||||
global.set_shape_id(err_shape);
|
||||
global.ensure_hash_props().push(Box::new(err_val));
|
||||
global.bump_generation();
|
||||
|
||||
{
|
||||
let err_ctor_ptr = kernel.builtin_world().error_constructor.as_ptr() as *mut JsObject;
|
||||
let err_ctor = unsafe { &mut *err_ctor_ptr };
|
||||
err_ctor.set_native_fn(Some(crate::builtins::error::error_constructor as *const ()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::builtin::FunctionMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
|
||||
pub fn bind_function(kernel: &Arc<OxideKernel>) {
|
||||
let function_methods = FunctionMethods {
|
||||
call: crate::builtins::function::function_call as *const (),
|
||||
apply: crate::builtins::function::function_apply as *const (),
|
||||
bind: crate::builtins::function::function_bind as *const (),
|
||||
};
|
||||
kernel.builtin_world().bind_function_methods(
|
||||
&function_methods,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_method;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
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_method!(
|
||||
kernel.builtin_world(),
|
||||
json,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
"parse",
|
||||
crate::builtins::json::json_parse,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
json,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
"stringify",
|
||||
crate::builtins::json::json_stringify,
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::bind_method;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
pub fn bind_map(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor_ptr = kernel.builtin_world().map_constructor.as_ptr() as *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);
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"set",
|
||||
crate::builtins::map::map_set,
|
||||
2
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"get",
|
||||
crate::builtins::map::map_get,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"has",
|
||||
crate::builtins::map::map_has,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"delete",
|
||||
crate::builtins::map::map_delete,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"clear",
|
||||
crate::builtins::map::map_clear,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"size",
|
||||
crate::builtins::map::map_size,
|
||||
0
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
kernel,
|
||||
global,
|
||||
"Map",
|
||||
ctor_ptr,
|
||||
crate::builtins::map::map_constructor,
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_kernel::{bind_method, bind_methods};
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
pub fn bind_math(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let math_ptr = kernel.builtin_world().math_object.as_ptr() as *mut JsObject;
|
||||
let math = unsafe { &mut *math_ptr };
|
||||
|
||||
let bw = kernel.builtin_world();
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
bind_methods!(
|
||||
bw,
|
||||
math,
|
||||
sf,
|
||||
sh,
|
||||
("abs", crate::builtins::math::math_abs, 1),
|
||||
("acos", crate::builtins::math::math_acos, 1),
|
||||
("asin", crate::builtins::math::math_asin, 1),
|
||||
("atan", crate::builtins::math::math_atan, 1),
|
||||
("atan2", crate::builtins::math::math_atan2, 2),
|
||||
("cbrt", crate::builtins::math::math_cbrt, 1),
|
||||
("ceil", crate::builtins::math::math_ceil, 1),
|
||||
("cos", crate::builtins::math::math_cos, 1),
|
||||
("cosh", crate::builtins::math::math_cosh, 1),
|
||||
("exp", crate::builtins::math::math_exp, 1),
|
||||
("floor", crate::builtins::math::math_floor, 1),
|
||||
("hypot", crate::builtins::math::math_hypot, 2),
|
||||
("imul", crate::builtins::math::math_imul, 2),
|
||||
("log", crate::builtins::math::math_log, 1),
|
||||
("log10", crate::builtins::math::math_log10, 1),
|
||||
("log2", crate::builtins::math::math_log2, 1),
|
||||
("max", crate::builtins::math::math_max, 2),
|
||||
("min", crate::builtins::math::math_min, 2),
|
||||
("pow", crate::builtins::math::math_pow, 2),
|
||||
("random", crate::builtins::math::math_random, 0),
|
||||
("round", crate::builtins::math::math_round, 1),
|
||||
("sign", crate::builtins::math::math_sign, 1),
|
||||
("sin", crate::builtins::math::math_sin, 1),
|
||||
("sinh", crate::builtins::math::math_sinh, 1),
|
||||
("sqrt", crate::builtins::math::math_sqrt, 1),
|
||||
("tan", crate::builtins::math::math_tan, 1),
|
||||
("tanh", crate::builtins::math::math_tanh, 1),
|
||||
("trunc", crate::builtins::math::math_trunc, 1),
|
||||
("acosh", crate::builtins::math::math_acosh, 1),
|
||||
("asinh", crate::builtins::math::math_asinh, 1),
|
||||
("atanh", crate::builtins::math::math_atanh, 1),
|
||||
("clz32", crate::builtins::math::math_clz32, 1),
|
||||
("expm1", crate::builtins::math::math_expm1, 1),
|
||||
("fround", crate::builtins::math::math_fround, 1),
|
||||
("log1p", crate::builtins::math::math_log1p, 1),
|
||||
);
|
||||
|
||||
for (name, val) in [
|
||||
("PI", std::f64::consts::PI),
|
||||
("E", std::f64::consts::E),
|
||||
("LN10", std::f64::consts::LN_10),
|
||||
("LN2", std::f64::consts::LN_2),
|
||||
("LOG10E", std::f64::consts::LOG10_E),
|
||||
("LOG2E", std::f64::consts::LOG2_E),
|
||||
("SQRT1_2", std::f64::consts::FRAC_1_SQRT_2),
|
||||
("SQRT2", std::f64::consts::SQRT_2),
|
||||
] {
|
||||
let si = kernel.string_forge().as_ref().intern(name).0;
|
||||
let sh_c = kernel
|
||||
.shape_forge()
|
||||
.as_ref()
|
||||
.make_shape(math.shape_id(), si);
|
||||
math.set_shape_id(sh_c);
|
||||
math.ensure_hash_props().push(Box::new(JsValue::float(val)));
|
||||
}
|
||||
|
||||
let si_m = kernel.string_forge().intern("Math").0;
|
||||
let m_shape = kernel.shape_forge().make_shape(global.shape_id(), si_m);
|
||||
let m_val =
|
||||
JsValue::from_js_object(kernel.builtin_world().math_object.as_ptr() as *mut JsObject);
|
||||
global.set_shape_id(m_shape);
|
||||
global.ensure_hash_props().push(Box::new(m_val));
|
||||
global.bump_generation();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::bind_method;
|
||||
use oxide_kernel::builtin::NumberMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
pub fn bind_number(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let number_methods = NumberMethods {
|
||||
is_nan: crate::builtins::number::number_is_nan as *const (),
|
||||
is_finite: crate::builtins::number::number_is_finite as *const (),
|
||||
parse_int: crate::builtins::number::number_parse_int as *const (),
|
||||
parse_float: crate::builtins::number::number_parse_float as *const (),
|
||||
to_string: crate::builtins::number::number_to_string as *const (),
|
||||
to_fixed: crate::builtins::number::number_to_fixed as *const (),
|
||||
};
|
||||
kernel.builtin_world().bind_number_methods(
|
||||
&number_methods,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
|
||||
let si_num = kernel.string_forge().intern("Number").0;
|
||||
let num_shape = kernel.shape_forge().make_shape(global.shape_id(), si_num);
|
||||
let num_val = JsValue::from_js_object(
|
||||
kernel.builtin_world().number_constructor.as_ptr() as *mut JsObject
|
||||
);
|
||||
global.set_shape_id(num_shape);
|
||||
global.ensure_hash_props().push(Box::new(num_val));
|
||||
global.bump_generation();
|
||||
|
||||
{
|
||||
let num_ctor_ptr = kernel.builtin_world().number_constructor.as_ptr() as *mut JsObject;
|
||||
let num_ctor = unsafe { &mut *num_ctor_ptr };
|
||||
num_ctor.set_native_fn(Some(
|
||||
crate::builtins::number::number_constructor as *const (),
|
||||
));
|
||||
num_ctor.set_native_arg_count(1);
|
||||
}
|
||||
|
||||
let pi_fn = crate::builtins::number::number_parse_int as *const ();
|
||||
let pf_fn = crate::builtins::number::number_parse_float as *const ();
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
global,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
"parseInt",
|
||||
pi_fn,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
global,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
"parseFloat",
|
||||
pf_fn,
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor;
|
||||
use oxide_kernel::builtin::ObjectMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
pub fn bind_object(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let methods = ObjectMethods {
|
||||
keys: crate::builtins::object::object_keys as *const (),
|
||||
create: crate::builtins::object::object_create as *const (),
|
||||
assign: crate::builtins::object::object_assign as *const (),
|
||||
define_property: crate::builtins::object::object_define_property as *const (),
|
||||
get_own_property_descriptor: crate::builtins::object::object_get_own_property_descriptor
|
||||
as *const (),
|
||||
freeze: crate::builtins::object::object_freeze as *const (),
|
||||
seal: crate::builtins::object::object_seal as *const (),
|
||||
prevent_extensions: crate::builtins::object::object_prevent_extensions as *const (),
|
||||
is_frozen: crate::builtins::object::object_is_frozen as *const (),
|
||||
is_sealed: crate::builtins::object::object_is_sealed as *const (),
|
||||
is_extensible: crate::builtins::object::object_is_extensible as *const (),
|
||||
get_own_property_names: crate::builtins::object::object_get_own_property_names as *const (),
|
||||
};
|
||||
kernel.builtin_world().bind_object_methods(
|
||||
&methods,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
|
||||
let ctor_ptr = kernel.builtin_world().object_constructor.as_ptr() as *mut JsObject;
|
||||
bind_constructor!(
|
||||
kernel,
|
||||
global,
|
||||
"Object",
|
||||
ctor_ptr,
|
||||
crate::builtins::object::object_constructor,
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bind_constructor_hash;
|
||||
use oxide_kernel::bind_method;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
|
||||
pub fn bind_set(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let ctor_ptr = kernel.builtin_world().set_constructor.as_ptr() as *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);
|
||||
let proto = unsafe { &mut *proto_ptr };
|
||||
let sf = kernel.string_forge().as_ref();
|
||||
let sh = kernel.shape_forge().as_ref();
|
||||
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"add",
|
||||
crate::builtins::set::set_add,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"has",
|
||||
crate::builtins::set::set_has,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"delete",
|
||||
crate::builtins::set::set_delete,
|
||||
1
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"clear",
|
||||
crate::builtins::set::set_clear,
|
||||
0
|
||||
);
|
||||
bind_method!(
|
||||
kernel.builtin_world(),
|
||||
proto,
|
||||
sf,
|
||||
sh,
|
||||
"size",
|
||||
crate::builtins::set::set_size,
|
||||
0
|
||||
);
|
||||
|
||||
bind_constructor_hash!(
|
||||
kernel,
|
||||
global,
|
||||
"Set",
|
||||
ctor_ptr,
|
||||
crate::builtins::set::set_constructor,
|
||||
1
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::builtin::StringMethods;
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
pub fn bind_string(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
let string_methods = StringMethods {
|
||||
index_of: crate::builtins::string::string_index_of as *const (),
|
||||
includes: crate::builtins::string::string_includes as *const (),
|
||||
char_at: crate::builtins::string::string_char_at as *const (),
|
||||
char_code_at: crate::builtins::string::string_char_code_at as *const (),
|
||||
concat: crate::builtins::string::string_concat as *const (),
|
||||
slice: crate::builtins::string::string_slice as *const (),
|
||||
substring: crate::builtins::string::string_substring as *const (),
|
||||
to_upper_case: crate::builtins::string::string_to_upper_case as *const (),
|
||||
to_lower_case: crate::builtins::string::string_to_lower_case as *const (),
|
||||
trim: crate::builtins::string::string_trim as *const (),
|
||||
repeat: crate::builtins::string::string_repeat as *const (),
|
||||
pad_start: crate::builtins::string::string_pad_start as *const (),
|
||||
pad_end: crate::builtins::string::string_pad_end as *const (),
|
||||
starts_with: crate::builtins::string::string_starts_with as *const (),
|
||||
ends_with: crate::builtins::string::string_ends_with as *const (),
|
||||
split: crate::builtins::string::string_split as *const (),
|
||||
replace: crate::builtins::string::string_replace as *const (),
|
||||
match_fn: crate::builtins::string::string_match_fn as *const (),
|
||||
search: crate::builtins::string::string_search as *const (),
|
||||
};
|
||||
kernel.builtin_world().bind_string_methods(
|
||||
&string_methods,
|
||||
kernel.string_forge().as_ref(),
|
||||
kernel.shape_forge().as_ref(),
|
||||
);
|
||||
|
||||
let si_str = kernel.string_forge().intern("String").0;
|
||||
let str_shape = kernel.shape_forge().make_shape(global.shape_id(), si_str);
|
||||
let str_val = JsValue::from_js_object(
|
||||
kernel.builtin_world().string_constructor.as_ptr() as *mut JsObject
|
||||
);
|
||||
global.set_shape_id(str_shape);
|
||||
global.ensure_hash_props().push(Box::new(str_val));
|
||||
global.bump_generation();
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
pub mod bind_array;
|
||||
pub mod bind_boolean;
|
||||
pub mod bind_date;
|
||||
pub mod bind_error;
|
||||
pub mod bind_function;
|
||||
pub mod bind_json;
|
||||
pub mod bind_map;
|
||||
pub mod bind_math;
|
||||
pub mod bind_number;
|
||||
pub mod bind_object;
|
||||
pub mod bind_set;
|
||||
pub mod bind_string;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_kernel::kernel::OxideKernel;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! bind_constructor {
|
||||
($kernel:expr, $global:expr, $name:literal, $ctor_ptr:expr, $ctor_fn:path, $nargs:literal) => {{
|
||||
let si = $kernel.string_forge().intern($name).0;
|
||||
let shape = $kernel.shape_forge().make_shape($global.shape_id(), si);
|
||||
let val = $crate::JsValue::from_js_object($ctor_ptr);
|
||||
$global.set_shape_id(shape);
|
||||
$global.push_prop(val);
|
||||
let ctor = unsafe { &mut *$ctor_ptr };
|
||||
ctor.set_native_fn(Some($ctor_fn as *const ()));
|
||||
ctor.set_native_arg_count($nargs);
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! bind_constructor_hash {
|
||||
($kernel:expr, $global:expr, $name:literal, $ctor_ptr:expr, $ctor_fn:path, $nargs:literal) => {{
|
||||
let si = $kernel.string_forge().intern($name).0;
|
||||
let shape = $kernel.shape_forge().make_shape($global.shape_id(), si);
|
||||
let val = $crate::JsValue::from_js_object($ctor_ptr);
|
||||
$global.set_shape_id(shape);
|
||||
$global.ensure_hash_props().push(Box::new(val));
|
||||
$global.bump_generation();
|
||||
let ctor = unsafe { &mut *$ctor_ptr };
|
||||
ctor.set_native_fn(Some($ctor_fn as *const ()));
|
||||
ctor.set_native_arg_count($nargs);
|
||||
}};
|
||||
}
|
||||
|
||||
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 };
|
||||
|
||||
bind_object::bind_object(kernel, global);
|
||||
bind_array::bind_array(kernel, global);
|
||||
bind_error::bind_error(kernel, global);
|
||||
bind_string::bind_string(kernel, global);
|
||||
bind_number::bind_number(kernel, global);
|
||||
bind_math::bind_math(kernel, global);
|
||||
bind_json::bind_json(kernel, global);
|
||||
bind_date::bind_date(kernel, global);
|
||||
bind_set::bind_set(kernel, global);
|
||||
bind_map::bind_map(kernel, global);
|
||||
bind_boolean::bind_boolean(kernel, global);
|
||||
bind_function::bind_function(kernel);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1,57 @@
|
||||
use crate::native::NativeFn;
|
||||
use crate::vm::Vm;
|
||||
use oxide_types::object::JsObject;
|
||||
use oxide_types::value::JsValue;
|
||||
|
||||
impl Vm {
|
||||
#[inline(always)]
|
||||
pub(crate) fn build_native_args(
|
||||
first_arg_reg: u8,
|
||||
arg_count: usize,
|
||||
this_reg: u8,
|
||||
) -> ([u8; 257], usize) {
|
||||
let mut args_buf = [0u8; 257];
|
||||
args_buf[0] = this_reg;
|
||||
let n = arg_count.min(256);
|
||||
for i in 0..n {
|
||||
args_buf[i + 1] = first_arg_reg.wrapping_add(i as u8);
|
||||
}
|
||||
(args_buf, n + 1)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn dispatch_native_call(
|
||||
&mut self,
|
||||
obj: &JsObject,
|
||||
callee: JsValue,
|
||||
this_reg: u8,
|
||||
first_arg_reg: u8,
|
||||
arg_count: usize,
|
||||
) -> Result<(), String> {
|
||||
let (args_buf, len) = Self::build_native_args(first_arg_reg, arg_count, this_reg);
|
||||
let args_slice = &args_buf[..len];
|
||||
|
||||
let func: NativeFn = unsafe { std::mem::transmute(obj.native_fn().unwrap()) };
|
||||
self.regs[254] = callee;
|
||||
match func(self, args_slice) {
|
||||
Ok(val) => {
|
||||
self.regs[0] = val;
|
||||
Ok(())
|
||||
}
|
||||
Err(err_val) => {
|
||||
let msg = if err_val.is_string() {
|
||||
self.kernel()
|
||||
.string_forge()
|
||||
.lookup(err_val.as_string_index())
|
||||
.unwrap_or_else(|| format!("{err_val}"))
|
||||
} else {
|
||||
format!("{err_val}")
|
||||
};
|
||||
let error = crate::builtins::error::create_error(self, &msg);
|
||||
self.exception_value = Some(error);
|
||||
self.pending_error_kind = Some("Error");
|
||||
self.unwind()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -0,0 +1,9 @@
|
||||
pub(crate) mod arithmetic;
|
||||
pub(crate) mod call;
|
||||
pub(crate) mod comparison;
|
||||
pub(crate) mod control_flow;
|
||||
pub(crate) mod exception;
|
||||
pub(crate) mod for_in;
|
||||
pub(crate) mod member;
|
||||
pub(crate) mod misc;
|
||||
pub(crate) mod property;
|
||||
@@ -0,0 +1 @@
|
||||
// dispatch helper functions extracted from vm.rs
|
||||
@@ -1,7 +1,9 @@
|
||||
#![doc = "OxideJS - Register-based VM with epoch arena memory"]
|
||||
|
||||
pub mod bindings;
|
||||
pub mod builtins;
|
||||
pub mod coercion;
|
||||
mod dispatch;
|
||||
pub mod native;
|
||||
pub mod vm;
|
||||
pub mod vm_pool;
|
||||
|
||||
+119
-1393
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user