8000 Share duplicate code between Wasm and the others · ruby/ruby@10bf69f · GitHub
[go: up one dir, main page]

Skip to content

Commit 10bf69f

Browse files
committed
Share duplicate code between Wasm and the others
1 parent 1a83474 commit 10bf69f

File tree

1 file changed

+42
-58
lines changed

1 file changed

+42
-58
lines changed

vm.c

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,113 +2299,97 @@ hook_before_rewind(rb_execution_context_t *ec, const rb_control_frame_t *cfp,
22992299
static inline VALUE
23002300
vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state, VALUE errinfo);
23012301

2302-
// for non-Emscripten Wasm build, use vm_exec with optimized setjmp for runtime performance
2303-
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2304-
2305-
struct rb_vm_exec_context {
2306-
rb_execution_context_t *ec;
2307-
struct rb_vm_tag *tag;
2308-
VALUE result;
2309-
enum ruby_tag_type state;
2310-
};
2311-
2312-
static void
2313-
vm_exec_enter_vm_loop(rb_execution_context_t *ec, struct rb_vm_exec_context *ctx,
2314-
struct rb_vm_tag *_tag, bool skip_first_ex_handle)
2302+
static VALUE
2303+
vm_exec_loop(rb_execution_context_t *ec, enum ruby_tag_type state,
2304+
struct rb_vm_tag *_tag, VALUE result)
23152305
{
2316-
if (skip_first_ex_handle) {
2306+
if (state == TAG_NONE) { /* no jumps, result is discarded */
23172307
goto vm_loop_start;
23182308
}
23192309

2320-
ctx->result = ec->errinfo;
23212310
rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2322-
while (UNDEF_P(ctx->result = vm_exec_handle_exception(ec, ctx->state, ctx->result))) {
2311+
while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result))) {
23232312
/* caught a jump, exec the handler */
2324-
ctx->result = vm_exec_core(ec);
2325-
vm_loop_start:
2313+
result = vm_exec_core(ec);
2314+
vm_loop_start:
23262315
VM_ASSERT(ec->tag == _tag);
23272316
/* when caught `throw`, `tag.state` is set. */
2328-
if ((ctx->state = _tag->state) == TAG_NONE) break;
2317+
if ((state = _tag->state) == TAG_NONE) break;
23292318
_tag->state = TAG_NONE;
23302319
}
2320+
2321+
return result;
23312322
}
23322323

2324+
struct rb_vm_exec_context {
2325+
rb_execution_context_t *const ec;
2326+
struct rb_vm_tag *const tag;
2327+
2328+
VALUE result;
2329+
};
2330+
2331+
// for non-Emscripten Wasm build, use vm_exec with optimized setjmp for runtime performance
2332+
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2333+
23332334
static void
23342335
vm_exec_bottom_main(void *context)
23352336
{
2336-
struct rb_vm_exec_context *ctx = (struct rb_vm_exec_context *)context;
2337+
struct rb_vm_exec_context *ctx = context;
2338+
rb_execution_context_t *ec = ctx->ec;
23372339

2338-
ctx->state = TAG_NONE;
2339-
ctx->result = vm_exec_core(ctx->ec);
2340-
vm_exec_enter_vm_loop(ctx->ec, ctx, ctx->tag, true);
2340+
ctx->result = vm_exec_loop(ec, TAG_NONE, ctx->tag, vm_exec_core(ec));
23412341
}
23422342

23432343
static void
23442344
vm_exec_bottom_rescue(void *context)
23452345
{
2346-
struct rb_vm_exec_context *ctx = (struct rb_vm_exec_context *)context;
2347-
ctx->state = rb_ec_tag_state(ctx->ec);
2348-
vm_exec_enter_vm_loop(ctx->ec, ctx, ctx->tag, false);
2346+
struct rb_vm_exec_context *ctx = context;
2347+
rb_execution_context_t *ec = ctx->ec;
2348+
2349+
ctx->result = vm_exec_loop(ec, rb_ec_tag_state(ec), ctx->tag, ec->errinfo);
23492350
}
2351+
#endif
23502352

23512353
VALUE
23522354
vm_exec(rb_execution_context_t *ec)
23532355
{
2354-
struct rb_vm_exec_context ctx = {
2355-
.ec = ec,
2356-
.result = Qundef,
2357-
};
2358-
struct rb_wasm_try_catch try_catch;
2356+
VALUE result = Qundef;
23592357

23602358
EC_PUSH_TAG(ec);
23612359

23622360
_tag.retval = Qnil;
2363-
ctx.tag = &_tag;
2361+
2362+
#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2363+
struct rb_vm_exec_context ctx = {
2364+
.ec = ec,
2365+
.tag = &_tag,
2366+
};
2367+
struct rb_wasm_try_catch try_catch;
23642368

23652369
EC_REPUSH_TAG();
23662370

23672371
rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx);
23682372

23692373
rb_wasm_try_catch_loop_run(&try_catch, &_tag.buf);
23702374

2371-
EC_POP_TAG();
2372-
return ctx.result;
2373-
}
2374-
2375+
result = ctx.result;
23752376
#else
2376-
2377-
VALUE
2378-
vm_exec(rb_execution_context_t *ec)
2379-
{
23802377
enum ruby_tag_type state;
2381-
VALUE result = Qundef;
2382-
2383-
EC_PUSH_TAG(ec);
2384-
2385-
_tag.retval = Qnil;
23862378
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
23872379
if (UNDEF_P(result = jit_exec(ec))) {
23882380
result = vm_exec_core(ec);
23892381
}
2390-
goto vm_loop_start; /* fallback to the VM */
2382+
/* fallback to the VM */
2383+
result = vm_exec_loop(ec, TAG_NONE, &_tag, result);
23912384
}
23922385
else {
2393-
result = ec->errinfo;
2394-
rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2395-
while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result))) {
2396-
/* caught a jump, exec the handler */
2397-
result = vm_exec_core(ec);
2398-
vm_loop_start:
2399-
VM_ASSERT(ec->tag == &_tag);
2400-
/* when caught `throw`, `tag.state` is set. */
2401-
if ((state = _tag.state) == TAG_NONE) break;
2402-
_tag.state = TAG_NONE;
2403-
}
2386+
result = vm_exec_loop(ec, state, &_tag, ec->errinfo);
24042387
}
2388+
#endif
2389+
24052390
EC_POP_TAG();
24062391
return result;
24072392
}
2408-
#endif
24092393

24102394
static inline VALUE
24112395
vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state, VALUE errinfo)

0 commit comments

Comments
 (0)
0