Skip to content

Commit

Permalink
Merge pull request #468 from ruby/katei/irb-crash
Browse files Browse the repository at this point in the history
Fix missing Asyncify-loop on exposed entry points
  • Loading branch information
kateinoigakukun authored Jun 15, 2024
2 parents f3a0599 + 5c1fed0 commit 1b4a243
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/gems/js/ext/js/js-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ static VALUE _rb_js_import_from_js(VALUE obj) {
* Returns +obj+ wrapped by JS class RbValue.
*/
static VALUE _rb_js_obj_wrap(VALUE obj, VALUE wrapping) {
#if JS_ENABLE_COMPONENT_MODEL
#ifdef JS_ENABLE_COMPONENT_MODEL
rb_abi_stage_rb_value_to_js(wrapping);
return jsvalue_s_new(rb_js_abi_host_rb_object_to_js_rb_value());
#else
Expand Down Expand Up @@ -511,7 +511,7 @@ static VALUE _rb_js_false_to_js(VALUE obj) {
* Returns +self+ as a JS::Object.
*/
static VALUE _rb_js_proc_to_js(VALUE obj) {
#if JS_ENABLE_COMPONENT_MODEL
#ifdef JS_ENABLE_COMPONENT_MODEL
rb_abi_stage_rb_value_to_js(obj);
return jsvalue_s_new(ruby_js_js_runtime_proc_to_js_function());
#else
Expand Down
52 changes: 35 additions & 17 deletions packages/gems/js/ext/js/witapi-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
#include "ruby.h"
#include "ruby/version.h"

static VALUE rb_eval_string_value_protect_thunk(VALUE str) {
#include "types.h"

static VALUE rb_eval_string_value_protect_thunk(VALUE ctx) {
const rb_abi_guest_string_t *cabi_str = (const rb_abi_guest_string_t *)ctx;
VALUE str = rb_utf8_str_new((const char *)cabi_str->ptr, cabi_str->len);
const ID id_eval = rb_intern("eval");
VALUE binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
const VALUE file = rb_utf8_str_new("eval", 4);
VALUE args[3] = {str, binding, file};
return rb_funcallv(rb_mKernel, id_eval, 3, args);
}

static VALUE rb_eval_string_value_protect(VALUE str, int *pstate) {
return rb_protect(rb_eval_string_value_protect_thunk, str, pstate);
static VALUE rb_eval_string_value_protect(const rb_abi_guest_string_t *str,
int *pstate) {
return rb_protect(rb_eval_string_value_protect_thunk, (VALUE)str, pstate);
}

#define TAG_NONE 0

#include "types.h"

__attribute__((import_module("asyncify"), import_name("start_unwind"))) void
asyncify_start_unwind(void *buf);
#define asyncify_start_unwind(buf) \
Expand Down Expand Up @@ -200,15 +203,17 @@ void exports_ruby_js_ruby_runtime_rb_abi_value_destructor(

void rb_abi_guest_ruby_show_version(void) { ruby_show_version(); }

void rb_abi_guest_ruby_init(void) {
RB_WASM_LIB_RT(ruby_init())

__attribute__((noinline)) static void rb_abi_guest_ruby_init_thunk(void) {
ruby_init();
rb_abi_guest_arena_hash = rb_hash_new();
rb_abi_guest_refcount_hash = rb_hash_new();

rb_gc_register_mark_object(rb_abi_guest_arena_hash);
rb_gc_register_mark_object(rb_abi_guest_refcount_hash);
}
void rb_abi_guest_ruby_init(void) {
RB_WASM_LIB_RT(rb_abi_guest_ruby_init_thunk())
}

void rb_abi_guest_ruby_sysinit(rb_abi_guest_list_string_t *args) {
char **c_args;
Expand All @@ -234,12 +239,11 @@ void rb_abi_guest_ruby_init_loadpath(void) {
RB_WASM_LIB_RT(ruby_init_loadpath())
}

void rb_abi_guest_rb_eval_string_protect(
__attribute__((noinline)) static void rb_abi_guest_rb_eval_string_protect_thunk(
rb_abi_guest_string_t *str, rb_abi_guest_tuple2_rb_abi_value_s32_t *ret0) {
VALUE retval;
RB_WASM_DEBUG_LOG("rb_eval_string_protect: str = %s\n", str->ptr);
VALUE utf8_str = rb_utf8_str_new((const char *)str->ptr, str->len);
RB_WASM_LIB_RT(retval = rb_eval_string_value_protect(utf8_str, &ret0->f1));
retval = rb_eval_string_value_protect(str, &ret0->f1);
RB_WASM_DEBUG_LOG("rb_eval_string_protect: retval = %p, state = %d\n",
(void *)retval, ret0->f1);

Expand All @@ -248,6 +252,10 @@ void rb_abi_guest_rb_eval_string_protect(
}
ret0->f0 = rb_abi_guest_rb_abi_value_new((void *)retval);
}
void rb_abi_guest_rb_eval_string_protect(
rb_abi_guest_string_t *str, rb_abi_guest_tuple2_rb_abi_value_s32_t *ret0) {
RB_WASM_LIB_RT(rb_abi_guest_rb_eval_string_protect_thunk(str, ret0));
}

struct rb_funcallv_thunk_ctx {
VALUE recv;
Expand Down Expand Up @@ -284,7 +292,9 @@ void rb_abi_guest_rb_funcallv_protect(
}

rb_abi_guest_rb_id_t rb_abi_guest_rb_intern(rb_abi_guest_string_t *name) {
return rb_intern((const char *)name->ptr);
VALUE retval;
RB_WASM_LIB_RT(retval = rb_intern((const char *)name->ptr));
return (rb_abi_guest_rb_id_t)retval;
}

rb_abi_guest_own_rb_abi_value_t rb_abi_guest_rb_errinfo(void) {
Expand All @@ -294,16 +304,24 @@ rb_abi_guest_own_rb_abi_value_t rb_abi_guest_rb_errinfo(void) {
return rb_abi_guest_rb_abi_value_new((void *)retval);
}

void rb_abi_guest_rb_clear_errinfo(void) { rb_set_errinfo(Qnil); }
void rb_abi_guest_rb_clear_errinfo(void) {
RB_WASM_LIB_RT(rb_set_errinfo(Qnil));
}

void rb_abi_guest_rstring_ptr(rb_abi_guest_rb_abi_value_t value,
rb_abi_guest_string_t *ret0) {
__attribute__((noinline)) static void
rb_abi_guest_rstring_ptr_thunk(rb_abi_guest_rb_abi_value_t value,
rb_abi_guest_string_t *ret0) {
VALUE r_str = (VALUE)rb_abi_guest_rb_abi_value_get(&value);
ret0->len = RSTRING_LEN(r_str);
ret0->ptr = xmalloc(ret0->len);
memcpy(ret0->ptr, RSTRING_PTR(r_str), ret0->len);
}

void rb_abi_guest_rstring_ptr(rb_abi_guest_rb_abi_value_t value,
rb_abi_guest_string_t *ret0) {
RB_WASM_LIB_RT(rb_abi_guest_rstring_ptr_thunk(value, ret0));
}

uint32_t rb_abi_guest_rb_abi_value_data_ptr(rb_abi_guest_rb_abi_value_t self) {
VALUE obj = (VALUE)rb_abi_guest_rb_abi_value_get(&self);
return (uint32_t)DATA_PTR(obj);
Expand Down Expand Up @@ -340,6 +358,8 @@ bool rb_abi_guest_rb_set_should_prohibit_rewind(bool value) {
return old;
}

#ifdef JS_ENABLE_COMPONENT_MODEL

static VALUE rb_abi_export_stage = Qnil;
static rb_abi_guest_own_rb_abi_value_t rb_abi_export_rb_value_to_js(void) {
VALUE staged = rb_abi_export_stage;
Expand All @@ -354,8 +374,6 @@ void rb_abi_stage_rb_value_to_js(VALUE value) {
rb_abi_export_stage = value;
}

#ifdef JS_ENABLE_COMPONENT_MODEL

extern void __wasm_call_ctors(void);
static inline void __wasm_call_ctors_if_needed(void) {
static bool __wasm_call_ctors_done = false;
Expand Down

0 comments on commit 1b4a243

Please sign in to comment.