feat: 支持 JSON 成员方法

This commit is contained in:
2026-06-07 23:41:52 +08:00
parent 531d1f2ba0
commit fe1cce5fbe
8 changed files with 572 additions and 1 deletions
+33
View File
@@ -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);
}