From 28cc1255f42dc17bff967d383543c20c355171db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Tue, 18 Mar 2025 18:49:34 -0400 Subject: [PATCH] Complete the table bindings for binding-sandbox --- binding-sandbox/table-binding.h | 106 ++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/binding-sandbox/table-binding.h b/binding-sandbox/table-binding.h index 60460dc5..00da84c2 100644 --- a/binding-sandbox/table-binding.h +++ b/binding-sandbox/table-binding.h @@ -35,6 +35,106 @@ namespace mkxp_sandbox { SANDBOX_DEF_DFREE(Table) SANDBOX_DEF_LOAD(Table) + static VALUE initialize(int32_t argc, wasm_ptr_t argv, VALUE self) { + SANDBOX_COROUTINE(coro, + int32_t x; + int32_t y; + int32_t z; + + VALUE operator()(int32_t argc, wasm_ptr_t argv, VALUE self) { + BOOST_ASIO_CORO_REENTER (this) { + y = z = 1; + + // TODO: throw error if too many or too few arguments + SANDBOX_AWAIT_AND_SET(x, rb_num2int, ((VALUE *)(**sb() + argv))[0]); + x = std::max(x, 0); + if (argc >= 2) { + SANDBOX_AWAIT_AND_SET(y, rb_num2int, ((VALUE *)(**sb() + argv))[1]); + y = std::max(y, 0); + if (argc >= 3) { + SANDBOX_AWAIT_AND_SET(z, rb_num2int, ((VALUE *)(**sb() + argv))[2]); + z = std::max(z, 0); + } + } + + Table *table = get_private_data(self); + if (table != NULL) { + table->resize(x, y, z); + } else { + table = new Table(x, y, z); + } + set_private_data(self, table); + } + + return SANDBOX_NIL; + } + ) + + return sb()->bind()()(argc, argv, self); + } + + static VALUE initialize_copy(VALUE self, VALUE obj) { + SANDBOX_COROUTINE(coro, + VALUE operator()(VALUE self, VALUE obj) { + BOOST_ASIO_CORO_REENTER (this) { + if (self != obj) { + SANDBOX_AWAIT(rb_obj_init_copy, self, obj); + set_private_data(self, new Table(*get_private_data
(obj))); + } + } + + return self; + } + ) + + return sb()->bind()()(self, obj); + } + + static VALUE resize(int32_t argc, wasm_ptr_t argv, VALUE self) { + SANDBOX_COROUTINE(coro, + Table *table; + int32_t x; + int32_t y; + int32_t z; + + VALUE operator()(int32_t argc, wasm_ptr_t argv, VALUE self) { + BOOST_ASIO_CORO_REENTER (this) { + y = z = 1; + + // TODO: throw error if too many or too few arguments + SANDBOX_AWAIT_AND_SET(x, rb_num2int, ((VALUE *)(**sb() + argv))[0]); + x = std::max(x, 0); + if (argc >= 2) { + SANDBOX_AWAIT_AND_SET(y, rb_num2int, ((VALUE *)(**sb() + argv))[1]); + y = std::max(y, 0); + if (argc >= 3) { + SANDBOX_AWAIT_AND_SET(z, rb_num2int, ((VALUE *)(**sb() + argv))[2]); + z = std::max(z, 0); + } + } + + get_private_data
(self)->resize(x, y, z); + } + + return SANDBOX_NIL; + } + ) + + return sb()->bind()()(argc, argv, self); + } + + static VALUE xsize(VALUE self) { + return sb()->bind()()(get_private_data
(self)->xSize()); + } + + static VALUE ysize(VALUE self) { + return sb()->bind()()(get_private_data
(self)->ySize()); + } + + static VALUE zsize(VALUE self) { + return sb()->bind()()(get_private_data
(self)->zSize()); + } + static VALUE get(int32_t argc, wasm_ptr_t argv, VALUE self) { SANDBOX_COROUTINE(coro, Table *table; @@ -115,6 +215,12 @@ namespace mkxp_sandbox { SANDBOX_AWAIT_AND_SET(klass, rb_define_class, "Table", sb()->rb_cObject()); SANDBOX_AWAIT(rb_define_alloc_func, klass, alloc); SANDBOX_AWAIT(rb_define_singleton_method, klass, "_load", (VALUE (*)(ANYARGS))load, 1); + SANDBOX_AWAIT(rb_define_method, klass, "initialize", (VALUE (*)(ANYARGS))initialize, -1); + SANDBOX_AWAIT(rb_define_method, klass, "initialize_copy", (VALUE (*)(ANYARGS))initialize_copy, 1); + SANDBOX_AWAIT(rb_define_method, klass, "resize", (VALUE (*)(ANYARGS))resize, -1); + SANDBOX_AWAIT(rb_define_method, klass, "xsize", (VALUE (*)(ANYARGS))xsize, 0); + SANDBOX_AWAIT(rb_define_method, klass, "ysize", (VALUE (*)(ANYARGS))ysize, 0); + SANDBOX_AWAIT(rb_define_method, klass, "zsize", (VALUE (*)(ANYARGS))zsize, 0); SANDBOX_AWAIT(rb_define_method, klass, "[]", (VALUE (*)(ANYARGS))get, -1); SANDBOX_AWAIT(rb_define_method, klass, "[]=", (VALUE (*)(ANYARGS))set, -1); }