mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 19:21:11 +08:00
feat: 支持 JSON 成员方法
This commit is contained in:
@@ -670,6 +670,38 @@ fn bind_math(kernel: &Arc<OxideKernel>, global: &mut JsObject) {
|
||||
global.bump_generation();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
fn bind_function(kernel: &Arc<OxideKernel>) {
|
||||
let function_methods = FunctionMethods {
|
||||
call: crate::builtins::function::function_call as *const (),
|
||||
@@ -693,6 +725,7 @@ pub fn init_kernel_builtins(kernel: &Arc<OxideKernel>) {
|
||||
bind_string(kernel, global);
|
||||
bind_number(kernel, global);
|
||||
bind_math(kernel, global);
|
||||
bind_json(kernel, global);
|
||||
bind_function(kernel);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user