Skip to content

Commit 878cf50

Browse files
rake format
1 parent 05f11d6 commit 878cf50

File tree

13 files changed

+95
-71
lines changed

13 files changed

+95
-71
lines changed

ext/js/lib/js.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def await(promise)
5050
->(value) { current.transfer(value, :success) },
5151
->(value) { current.transfer(value, :failure) }
5252
)
53-
raise "JS::Object#await can be called only from evalAsync" if @loop == current
53+
if @loop == current
54+
raise "JS::Object#await can be called only from evalAsync"
55+
end
5456
value, status = @loop.transfer
5557
raise JS::Error.new(value) if status == :failure
5658
value

ext/witapi/witapi-core.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ __attribute__((import_module("asyncify"), import_name("stop_unwind"))) void
6363
asyncify_stop_unwind(void);
6464
__attribute__((import_module("asyncify"), import_name("start_rewind"))) void
6565
asyncify_start_rewind(void *buf);
66-
#define asyncify_start_rewind(buf) \
67-
asyncify_start_rewind((buf))
66+
#define asyncify_start_rewind(buf) asyncify_start_rewind((buf))
6867
__attribute__((import_module("asyncify"), import_name("stop_rewind"))) void
6968
asyncify_stop_rewind(void);
7069

@@ -89,9 +88,9 @@ __attribute__((noreturn)) void
8988
rb_wasm_throw_prohibit_rewind_exception(const char *c_msg, size_t msg_len);
9089

9190
#define RB_WASM_CHECK_REWIND_PROHIBITED(msg) \
92-
/*
93-
If the unwond source and rewinding destination are same, it's acceptable
94-
to rewind even under nested VM operations.
91+
/* \
92+
If the unwond source and rewinding destination are same, it's acceptable \
93+
to rewind even under nested VM operations. \
9594
*/ \
9695
if (rb_should_prohibit_rewind && \
9796
(asyncify_buf != asyncify_unwound_buf || fiber_entry_point)) { \
@@ -329,9 +328,7 @@ void rb_vm_bugreport(const void *);
329328

330329
void rb_abi_guest_rb_vm_bugreport(void) { rb_vm_bugreport(NULL); }
331330

332-
bool rb_abi_guest_rb_gc_enable(void) {
333-
return rb_gc_enable() == Qtrue;
334-
}
331+
bool rb_abi_guest_rb_gc_enable(void) { return rb_gc_enable() == Qtrue; }
335332

336333
VALUE rb_gc_disable_no_rest(void);
337334
bool rb_abi_guest_rb_gc_disable(void) {

packages/npm-packages/ruby-wasm-wasi/rollup.config.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ function variant(basename, { browser = false } = {}) {
2626
},
2727
],
2828
plugins: [
29-
...(browser ? [
30-
nodePolyfills(),
31-
inject({ Buffer: ['buffer', 'Buffer']}),
32-
] : []),
29+
...(browser
30+
? [nodePolyfills(), inject({ Buffer: ["buffer", "Buffer"] })]
31+
: []),
3332
typescript(typescriptOptions),
3433
nodeResolve(),
3534
],

packages/npm-packages/ruby-wasm-wasi/src/browser.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ const consolePrinter = () => {
99

1010
return {
1111
addToImports(imports: WebAssembly.Imports): void {
12-
const original = imports.wasi_snapshot_preview1.fd_write as (fd: number, iovs: number, iovsLen: number, nwritten: number) => number;
13-
imports.wasi_snapshot_preview1.fd_write = (fd: number, iovs: number, iovsLen: number, nwritten: number): number => {
12+
const original = imports.wasi_snapshot_preview1.fd_write as (
13+
fd: number,
14+
iovs: number,
15+
iovsLen: number,
16+
nwritten: number
17+
) => number;
18+
imports.wasi_snapshot_preview1.fd_write = (
19+
fd: number,
20+
iovs: number,
21+
iovsLen: number,
22+
nwritten: number
23+
): number => {
1424
if (fd !== 1 && fd !== 2) {
1525
return original(fd, iovs, iovsLen, nwritten);
1626
}
1727

18-
if (typeof memory === 'undefined' || typeof view === 'undefined') {
19-
throw new Error('Memory is not set');
28+
if (typeof memory === "undefined" || typeof view === "undefined") {
29+
throw new Error("Memory is not set");
2030
}
2131
if (view.buffer.byteLength === 0) {
2232
view = new DataView(memory.buffer);
@@ -30,7 +40,7 @@ const consolePrinter = () => {
3040
});
3141

3242
let written = 0;
33-
let str = '';
43+
let str = "";
3444
for (const buffer of buffers) {
3545
str += decoder.decode(buffer);
3646
written += buffer.byteLength;
@@ -46,8 +56,8 @@ const consolePrinter = () => {
4656
setMemory(m: WebAssembly.Memory) {
4757
memory = m;
4858
view = new DataView(m.buffer);
49-
}
50-
}
59+
},
60+
};
5161
};
5262

5363
export const DefaultRubyVM = async (

packages/npm-packages/ruby-wasm-wasi/src/index.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,24 @@ export class RubyVM {
3232
private exceptionFormatter: RbExceptionFormatter;
3333
private interfaceState: RbAbiInterfaceState = {
3434
hasJSFrameAfterRbFrame: false,
35-
}
35+
};
3636

3737
constructor() {
3838
// Wrap exported functions from Ruby VM to prohibit nested VM operation
3939
// if the call stack has sandwitched JS frames like JS -> Ruby -> JS -> Ruby.
4040
const proxyExports = (exports: RbAbi.RbAbiGuest) => {
41-
const excludedMethods: (keyof RbAbi.RbAbiGuest)[] = ["addToImports", "instantiate", "rbSetShouldProhibitRewind", "rbGcDisable", "rbGcEnable"];
41+
const excludedMethods: (keyof RbAbi.RbAbiGuest)[] = [
42+
"addToImports",
43+
"instantiate",
44+
"rbSetShouldProhibitRewind",
45+
"rbGcDisable",
46+
"rbGcEnable",
47+
];
4248
const excluded = ["constructor"].concat(excludedMethods);
4349
// wrap all methods in RbAbi.RbAbiGuest class
44-
for (const key of Object.getOwnPropertyNames(RbAbi.RbAbiGuest.prototype)) {
50+
for (const key of Object.getOwnPropertyNames(
51+
RbAbi.RbAbiGuest.prototype
52+
)) {
4553
if (excluded.includes(key)) {
4654
continue;
4755
}
@@ -50,22 +58,23 @@ export class RubyVM {
5058
exports[key] = (...args: any[]) => {
5159
const isNestedVMCall = this.interfaceState.hasJSFrameAfterRbFrame;
5260
if (isNestedVMCall) {
53-
const oldShouldProhibitRewind = this.guest.rbSetShouldProhibitRewind(true)
54-
const oldIsDisabledGc = this.guest.rbGcDisable()
55-
const result = Reflect.apply(value, exports, args)
56-
this.guest.rbSetShouldProhibitRewind(oldShouldProhibitRewind)
61+
const oldShouldProhibitRewind =
62+
this.guest.rbSetShouldProhibitRewind(true);
63+
const oldIsDisabledGc = this.guest.rbGcDisable();
64+
const result = Reflect.apply(value, exports, args);
65+
this.guest.rbSetShouldProhibitRewind(oldShouldProhibitRewind);
5766
if (!oldIsDisabledGc) {
58-
this.guest.rbGcEnable()
67+
this.guest.rbGcEnable();
5968
}
6069
return result;
6170
} else {
62-
return Reflect.apply(value, exports, args)
71+
return Reflect.apply(value, exports, args);
6372
}
64-
}
73+
};
6574
}
6675
}
6776
return exports;
68-
}
77+
};
6978
this.guest = proxyExports(new RbAbi.RbAbiGuest());
7079
this.transport = new JsValueTransport();
7180
this.exceptionFormatter = new RbExceptionFormatter();
@@ -121,25 +130,29 @@ export class RubyVM {
121130
};
122131
}
123132
imports["rb-js-abi-host"] = {
124-
rb_wasm_throw_prohibit_rewind_exception: (messagePtr: number, messageLen: number) => {
133+
rb_wasm_throw_prohibit_rewind_exception: (
134+
messagePtr: number,
135+
messageLen: number
136+
) => {
125137
const memory = this.instance.exports.memory as WebAssembly.Memory;
126138
const str = new TextDecoder().decode(
127139
new Uint8Array(memory.buffer, messagePtr, messageLen)
128140
);
129141
throw new RbFatalError(
130-
"Ruby APIs that may rewind the VM stack are prohibited under nested VM operation " + `(${str})\n`
131-
+ "Nested VM operation means that the call stack has sandwitched JS frames like JS -> Ruby -> JS -> Ruby "
132-
+ "caused by something like `window.rubyVM.eval(\"JS.global[:rubyVM].eval('Fiber.yield')\")`\n"
133-
+ "\n"
134-
+ "Please check your call stack and make sure that you are **not** doing any of the following inside the nested Ruby frame:\n"
135-
+ " 1. Switching fibers (e.g. Fiber#resume, Fiber.yield, and Fiber#transfer)\n"
136-
+ " Note that JS::Object#await switches fibers internally\n"
137-
+ " 2. Raising uncaught exceptions\n"
138-
+ " Please catch all exceptions inside the nested operation\n"
139-
+ " 3. Calling Continuation APIs\n"
142+
"Ruby APIs that may rewind the VM stack are prohibited under nested VM operation " +
143+
`(${str})\n` +
144+
"Nested VM operation means that the call stack has sandwitched JS frames like JS -> Ruby -> JS -> Ruby " +
145+
"caused by something like `window.rubyVM.eval(\"JS.global[:rubyVM].eval('Fiber.yield')\")`\n" +
146+
"\n" +
147+
"Please check your call stack and make sure that you are **not** doing any of the following inside the nested Ruby frame:\n" +
148+
" 1. Switching fibers (e.g. Fiber#resume, Fiber.yield, and Fiber#transfer)\n" +
149+
" Note that JS::Object#await switches fibers internally\n" +
150+
" 2. Raising uncaught exceptions\n" +
151+
" Please catch all exceptions inside the nested operation\n" +
152+
" 3. Calling Continuation APIs\n"
140153
);
141-
}
142-
}
154+
},
155+
};
143156
// NOTE: The GC may collect objects that are still referenced by Wasm
144157
// locals because Asyncify cannot scan the Wasm stack above the JS frame.
145158
// So we need to keep track whether the JS frame is sandwitched by Ruby
@@ -153,7 +166,7 @@ export class RubyVM {
153166
const result = Reflect.apply(value, imports, args);
154167
this.interfaceState.hasJSFrameAfterRbFrame = oldValue;
155168
return result;
156-
}
169+
};
157170
}
158171
}
159172
return imports;
@@ -377,9 +390,9 @@ type RbAbiInterfaceState = {
377390
/**
378391
* Track if the last JS frame that was created by a Ruby frame
379392
* to determine if we have a sandwitched JS frame between Ruby frames.
380-
**/
393+
**/
381394
hasJSFrameAfterRbFrame: boolean;
382-
}
395+
};
383396

384397
/**
385398
* Export a JS value held by the Ruby VM to the JS environment.

packages/npm-packages/ruby-wasm-wasi/test/gc.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe("GC integration", () => {
106106
vm.eval("GC.start");
107107

108108
// Disable GC before the nested call
109-
vm.eval("GC.disable")
109+
vm.eval("GC.disable");
110110
const o2 = vm.eval(`
111111
JS.eval(<<~JS)
112112
return {

packages/npm-packages/ruby-wasm-wasi/test/package.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("Packaging validation", () => {
4545
const mod = await loadWasmModule(`ruby+stdlib.wasm`);
4646
const { vm } = await DefaultRubyVM(mod);
4747
vm.eval(`require "stringio"`);
48-
})
48+
});
4949

5050
test.each([
5151
{ file: "ruby+stdlib.wasm", stdlib: true },

packages/npm-packages/ruby-wasm-wasi/test/to_js.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ describe("RbValue#toJS", () => {
2929
const result = JS.call("eval", vm.eval(`"${props.expr}"`));
3030
expect(result.toJS()).toBe(props.result);
3131
});
32-
3332
});

packages/npm-packages/ruby-wasm-wasi/test/unit/test_async.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ def test_concurrent_promises
4545

4646
def test_await_in_fiber
4747
fiber_ended = false
48-
Fiber.new do
49-
promise = JS.eval("return Promise.resolve(42)")
50-
assert_equal 42, promise.await.to_i
51-
fiber_ended = true
52-
end.resume
48+
Fiber
49+
.new do
50+
promise = JS.eval("return Promise.resolve(42)")
51+
assert_equal 42, promise.await.to_i
52+
fiber_ended = true
53+
end
54+
.resume
5355
assert_equal true, fiber_ended
5456
end
5557
end

packages/npm-packages/ruby-wasm-wasi/test/unit/test_object_wrap.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def test_identity
88
JS
99
o1 = Object.new
1010
o1_clone = intrinsics.call(:identity, JS::Object.wrap(o1))
11-
assert_equal o1.object_id, o1_clone.call("call", "object_id").call("toJS").to_i
11+
assert_equal o1.object_id,
12+
o1_clone.call("call", "object_id").call("toJS").to_i
1213
end
1314
end

0 commit comments

Comments
 (0)