js 2.5.0 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ static VALUE rb_eval_string_value_protect(VALUE str, int *pstate) {
17
17
 
18
18
  #define TAG_NONE 0
19
19
 
20
- #include "bindgen/rb-abi-guest.h"
20
+ #include "types.h"
21
21
 
22
22
  __attribute__((import_module("asyncify"), import_name("start_unwind"))) void
23
23
  asyncify_start_unwind(void *buf);
@@ -55,10 +55,18 @@ void *rb_wasm_handle_fiber_unwind(void (**new_fiber_entry)(void *, void *),
55
55
 
56
56
  static bool rb_should_prohibit_rewind = false;
57
57
 
58
+ #ifdef JS_ENABLE_COMPONENT_MODEL
59
+ void rb_wasm_throw_prohibit_rewind_exception(const char *c_msg,
60
+ size_t msg_len) {
61
+ ext_string_t message = {.ptr = (uint8_t *)c_msg, .len = msg_len};
62
+ ruby_js_js_runtime_throw_prohibit_rewind_exception(&message);
63
+ }
64
+ #else
58
65
  __attribute__((import_module("rb-js-abi-host"),
59
66
  import_name("rb_wasm_throw_prohibit_rewind_exception")))
60
67
  __attribute__((noreturn)) void
61
68
  rb_wasm_throw_prohibit_rewind_exception(const char *c_msg, size_t msg_len);
69
+ #endif
62
70
 
63
71
  #define RB_WASM_CHECK_REWIND_PROHIBITED(msg) \
64
72
  /* \
@@ -122,7 +130,7 @@ rb_wasm_throw_prohibit_rewind_exception(const char *c_msg, size_t msg_len);
122
130
  { \
123
131
  new_args = alloca(sizeof(char *) * ((list)->len + 1)); \
124
132
  for (size_t i = 0; i < (list)->len; i++) { \
125
- new_args[i] = (list)->ptr[i].ptr; \
133
+ new_args[i] = (char *)((list)->ptr[i].ptr); \
126
134
  } \
127
135
  }
128
136
 
@@ -176,6 +184,16 @@ void rb_abi_guest_rb_abi_value_dtor(void *data) {
176
184
  assert(state == TAG_NONE && "rb_abi_guest_rb_abi_value_dtor_internal failed");
177
185
  }
178
186
 
187
+ #ifdef JS_ENABLE_COMPONENT_MODEL
188
+ void exports_ruby_js_ruby_runtime_rb_iseq_destructor(
189
+ exports_ruby_js_ruby_runtime_rb_iseq_t *rep) {}
190
+
191
+ void exports_ruby_js_ruby_runtime_rb_abi_value_destructor(
192
+ exports_ruby_js_ruby_runtime_rb_abi_value_t *rep) {
193
+ rb_abi_guest_rb_abi_value_dtor((void *)rep);
194
+ }
195
+ #endif
196
+
179
197
  // MARK: - Exported functions
180
198
  // NOTE: Assume that callers always pass null terminated string by
181
199
  // rb_abi_guest_string_t
@@ -209,7 +227,7 @@ rb_abi_guest_ruby_options(rb_abi_guest_list_string_t *args) {
209
227
  }
210
228
 
211
229
  void rb_abi_guest_ruby_script(rb_abi_guest_string_t *name) {
212
- RB_WASM_LIB_RT(ruby_script(name->ptr))
230
+ RB_WASM_LIB_RT(ruby_script((const char *)name->ptr))
213
231
  }
214
232
 
215
233
  void rb_abi_guest_ruby_init_loadpath(void) {
@@ -220,7 +238,7 @@ void rb_abi_guest_rb_eval_string_protect(
220
238
  rb_abi_guest_string_t *str, rb_abi_guest_tuple2_rb_abi_value_s32_t *ret0) {
221
239
  VALUE retval;
222
240
  RB_WASM_DEBUG_LOG("rb_eval_string_protect: str = %s\n", str->ptr);
223
- VALUE utf8_str = rb_utf8_str_new(str->ptr, str->len);
241
+ VALUE utf8_str = rb_utf8_str_new((const char *)str->ptr, str->len);
224
242
  RB_WASM_LIB_RT(retval = rb_eval_string_value_protect(utf8_str, &ret0->f1));
225
243
  RB_WASM_DEBUG_LOG("rb_eval_string_protect: retval = %p, state = %d\n",
226
244
  (void *)retval, ret0->f1);
@@ -266,10 +284,10 @@ void rb_abi_guest_rb_funcallv_protect(
266
284
  }
267
285
 
268
286
  rb_abi_guest_rb_id_t rb_abi_guest_rb_intern(rb_abi_guest_string_t *name) {
269
- return rb_intern(name->ptr);
287
+ return rb_intern((const char *)name->ptr);
270
288
  }
271
289
 
272
- rb_abi_guest_rb_abi_value_t rb_abi_guest_rb_errinfo(void) {
290
+ rb_abi_guest_own_rb_abi_value_t rb_abi_guest_rb_errinfo(void) {
273
291
  VALUE retval;
274
292
  RB_WASM_LIB_RT(retval = rb_errinfo());
275
293
  rb_abi_lend_object(retval);
@@ -322,4 +340,118 @@ bool rb_abi_guest_rb_set_should_prohibit_rewind(bool value) {
322
340
  return old;
323
341
  }
324
342
 
343
+ static VALUE rb_abi_export_stage = Qnil;
344
+ static rb_abi_guest_own_rb_abi_value_t rb_abi_export_rb_value_to_js(void) {
345
+ VALUE staged = rb_abi_export_stage;
346
+ rb_abi_export_stage = Qnil;
347
+ rb_abi_lend_object(staged);
348
+ return rb_abi_guest_rb_abi_value_new((void *)staged);
349
+ }
350
+
351
+ void rb_abi_stage_rb_value_to_js(VALUE value) {
352
+ assert(rb_abi_export_stage == Qnil &&
353
+ "rb_abi_stage_rb_value_to_js: stage is not empty!?");
354
+ rb_abi_export_stage = value;
355
+ }
356
+
357
+ #ifdef JS_ENABLE_COMPONENT_MODEL
358
+
359
+ extern void __wasm_call_ctors(void);
360
+ static inline void __wasm_call_ctors_if_needed(void) {
361
+ static bool __wasm_call_ctors_done = false;
362
+ if (!__wasm_call_ctors_done) {
363
+ __wasm_call_ctors_done = true;
364
+ __wasm_call_ctors();
365
+
366
+ __attribute__((weak)) extern void __wasi_vfs_rt_init(void);
367
+ if (__wasi_vfs_rt_init) {
368
+ __wasi_vfs_rt_init();
369
+ }
370
+ }
371
+ }
372
+
373
+ // Exported Functions from `ruby:js/ruby-runtime`
374
+ void exports_ruby_js_ruby_runtime_ruby_show_version(void) {
375
+ __wasm_call_ctors_if_needed();
376
+ rb_abi_guest_ruby_show_version();
377
+ }
378
+ void exports_ruby_js_ruby_runtime_ruby_init(void) {
379
+ __wasm_call_ctors_if_needed();
380
+ rb_abi_guest_ruby_init();
381
+ }
382
+ void exports_ruby_js_ruby_runtime_ruby_sysinit(ext_list_string_t *args) {
383
+ __wasm_call_ctors_if_needed();
384
+ rb_abi_guest_ruby_sysinit(args);
385
+ }
386
+ exports_ruby_js_ruby_runtime_own_rb_iseq_t
387
+ exports_ruby_js_ruby_runtime_ruby_options(ext_list_string_t *args) {
388
+ __wasm_call_ctors_if_needed();
389
+ return rb_abi_guest_ruby_options(args);
390
+ }
391
+ void exports_ruby_js_ruby_runtime_ruby_script(ext_string_t *name) {
392
+ __wasm_call_ctors_if_needed();
393
+ rb_abi_guest_ruby_script(name);
394
+ }
395
+ void exports_ruby_js_ruby_runtime_ruby_init_loadpath(void) {
396
+ __wasm_call_ctors_if_needed();
397
+ rb_abi_guest_ruby_init_loadpath();
398
+ }
399
+ void exports_ruby_js_ruby_runtime_rb_eval_string_protect(
400
+ ext_string_t *str,
401
+ exports_ruby_js_ruby_runtime_tuple2_own_rb_abi_value_s32_t *ret) {
402
+ __wasm_call_ctors_if_needed();
403
+ rb_abi_guest_rb_eval_string_protect(str, ret);
404
+ }
405
+ void exports_ruby_js_ruby_runtime_rb_funcallv_protect(
406
+ exports_ruby_js_ruby_runtime_borrow_rb_abi_value_t recv,
407
+ exports_ruby_js_ruby_runtime_rb_id_t mid,
408
+ exports_ruby_js_ruby_runtime_list_borrow_rb_abi_value_t *args,
409
+ exports_ruby_js_ruby_runtime_tuple2_own_rb_abi_value_s32_t *ret) {
410
+ __wasm_call_ctors_if_needed();
411
+ rb_abi_guest_rb_funcallv_protect(recv, mid, args, ret);
412
+ }
413
+ exports_ruby_js_ruby_runtime_rb_id_t
414
+ exports_ruby_js_ruby_runtime_rb_intern(ext_string_t *name) {
415
+ __wasm_call_ctors_if_needed();
416
+ return rb_abi_guest_rb_intern(name);
417
+ }
418
+ exports_ruby_js_ruby_runtime_own_rb_abi_value_t
419
+ exports_ruby_js_ruby_runtime_rb_errinfo(void) {
420
+ __wasm_call_ctors_if_needed();
421
+ return rb_abi_guest_rb_errinfo();
422
+ }
423
+ void exports_ruby_js_ruby_runtime_rb_clear_errinfo(void) {
424
+ __wasm_call_ctors_if_needed();
425
+ rb_abi_guest_rb_clear_errinfo();
426
+ }
427
+ void exports_ruby_js_ruby_runtime_rstring_ptr(
428
+ exports_ruby_js_ruby_runtime_borrow_rb_abi_value_t value,
429
+ ext_string_t *ret) {
430
+ __wasm_call_ctors_if_needed();
431
+ rb_abi_guest_rstring_ptr(value, ret);
432
+ }
433
+ void exports_ruby_js_ruby_runtime_rb_vm_bugreport(void) {
434
+ __wasm_call_ctors_if_needed();
435
+ rb_abi_guest_rb_vm_bugreport();
436
+ }
437
+ bool exports_ruby_js_ruby_runtime_rb_gc_enable(void) {
438
+ __wasm_call_ctors_if_needed();
439
+ return rb_abi_guest_rb_gc_enable();
440
+ }
441
+ bool exports_ruby_js_ruby_runtime_rb_gc_disable(void) {
442
+ __wasm_call_ctors_if_needed();
443
+ return rb_abi_guest_rb_gc_disable();
444
+ }
445
+ bool exports_ruby_js_ruby_runtime_rb_set_should_prohibit_rewind(
446
+ bool new_value) {
447
+ __wasm_call_ctors_if_needed();
448
+ return rb_abi_guest_rb_set_should_prohibit_rewind(new_value);
449
+ }
450
+ exports_ruby_js_ruby_runtime_own_rb_abi_value_t
451
+ exports_ruby_js_ruby_runtime_export_rb_value_to_js(void) {
452
+ __wasm_call_ctors_if_needed();
453
+ return rb_abi_export_rb_value_to_js();
454
+ }
455
+ #endif
456
+
325
457
  void Init_witapi(void) {}
data/js.gemspec CHANGED
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
25
25
  end
26
26
  end
27
27
  spec.require_paths = ["lib"]
28
- spec.extensions = ["ext/js/extconf.rb", "ext/witapi/extconf.rb"]
28
+ spec.extensions = ["ext/js/extconf.rb"]
29
29
  end
data/lib/js/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JS
2
- VERSION = "2.5.0"
2
+ VERSION = "2.5.2"
3
3
  end
data/lib/js.rb CHANGED
@@ -79,8 +79,8 @@ module JS
79
79
  current = Fiber.current
80
80
  promise.call(
81
81
  :then,
82
- ->(value) { current.transfer(value, :success) },
83
- ->(value) { current.transfer(value, :failure) }
82
+ ->(value) { current.transfer(value, :success); nil },
83
+ ->(value) { current.transfer(value, :failure); nil }
84
84
  )
85
85
  if @loop == current
86
86
  raise (
@@ -137,8 +137,10 @@ class JS::Object
137
137
  # JS.global[:Date].new(2020, 1, 1)
138
138
  # JS.global[:Error].new("error message")
139
139
  # JS.global[:URLSearchParams].new(JS.global[:location][:search])
140
+ # JS.global[:Promise].new ->(resolve, reject) { resolve.call(42) }
140
141
  #
141
- def new(*args)
142
+ def new(*args, &block)
143
+ args = args + [block] if block
142
144
  JS.global[:Reflect].construct(self, args.to_js)
143
145
  end
144
146
 
@@ -189,6 +191,20 @@ class JS::Object
189
191
  self[sym].typeof == "function"
190
192
  end
191
193
 
194
+ # Call the receiver (a JavaScript function) with `undefined` as its receiver context.
195
+ # This method is similar to JS::Object#call, but it is used to call a function that is not
196
+ # a method of an object.
197
+ #
198
+ # floor = JS.global[:Math][:floor]
199
+ # floor.apply(3.14) # => 3
200
+ # JS.global[:Promise].new do |resolve, reject|
201
+ # resolve.apply(42)
202
+ # end.await # => 42
203
+ def apply(*args, &block)
204
+ args = args + [block] if block
205
+ JS.global[:Reflect].call(:apply, self, JS::Undefined, args.to_js)
206
+ end
207
+
192
208
  # Await a JavaScript Promise like `await` in JavaScript.
193
209
  # This method looks like a synchronous method, but it actually runs asynchronously using fibers.
194
210
  # In other words, the next line to the `await` call at Ruby source will be executed after the
@@ -0,0 +1,45 @@
1
+ package ruby:js;
2
+
3
+ interface js-runtime {
4
+
5
+ resource js-abi-value {}
6
+
7
+ variant js-abi-result {
8
+ success(js-abi-value),
9
+ failure(js-abi-value),
10
+ }
11
+
12
+ eval-js: func(code: string) -> js-abi-result;
13
+ is-js: func(value: borrow<js-abi-value>) -> bool;
14
+ instance-of: func(value: borrow<js-abi-value>, klass: borrow<js-abi-value>) -> bool;
15
+ global-this: func() -> js-abi-value;
16
+ int-to-js-number: func(value: s32) -> js-abi-value;
17
+ float-to-js-number: func(value: float64) -> js-abi-value;
18
+ string-to-js-string: func(value: string) -> js-abi-value;
19
+ bool-to-js-bool: func(value: bool) -> js-abi-value;
20
+ proc-to-js-function: func() -> js-abi-value;
21
+ rb-object-to-js-rb-value: func() -> js-abi-value;
22
+
23
+ js-value-to-string: func(value: borrow<js-abi-value>) -> string;
24
+
25
+ variant raw-integer {
26
+ as-float(float64),
27
+ bignum(string),
28
+ }
29
+
30
+ js-value-to-integer: func(value: borrow<js-abi-value>) -> raw-integer;
31
+
32
+ export-js-value-to-host: func(value: borrow<js-abi-value>);
33
+ import-js-value-from-host: func() -> js-abi-value;
34
+
35
+ js-value-typeof: func(value: borrow<js-abi-value>) -> string;
36
+
37
+ js-value-equal: func(lhs: borrow<js-abi-value>, rhs: borrow<js-abi-value>) -> bool;
38
+ js-value-strictly-equal: func(lhs: borrow<js-abi-value>, rhs: borrow<js-abi-value>) -> bool;
39
+
40
+ reflect-apply: func(target: borrow<js-abi-value>, this-argument: borrow<js-abi-value>, arguments: list<borrow<js-abi-value>>) -> js-abi-result;
41
+ reflect-get: func(target: borrow<js-abi-value>, property-key: string) -> js-abi-result;
42
+ reflect-set: func(target: borrow<js-abi-value>, property-key: string, value: borrow<js-abi-value>) -> js-abi-result;
43
+
44
+ throw-prohibit-rewind-exception: func(message: string);
45
+ }
@@ -0,0 +1,43 @@
1
+ package ruby:js;
2
+
3
+ interface ruby-runtime {
4
+ use js-runtime.{js-abi-value};
5
+
6
+ resource rb-iseq {}
7
+ resource rb-abi-value {}
8
+
9
+ type rb-errno = s32;
10
+ type rb-id = u32;
11
+
12
+ ruby-show-version: func();
13
+ ruby-init: func();
14
+ ruby-sysinit: func(args: list<string>);
15
+ ruby-options: func(args: list<string>) -> rb-iseq;
16
+ ruby-script: func(name: string);
17
+ ruby-init-loadpath: func();
18
+ rb-eval-string-protect: func(str: string) -> tuple<rb-abi-value, s32>;
19
+ rb-funcallv-protect: func(recv: borrow<rb-abi-value>, mid: rb-id, args: list<borrow<rb-abi-value>>) -> tuple<rb-abi-value, s32>;
20
+ rb-intern: func(name: string) -> rb-id;
21
+ rb-errinfo: func() -> rb-abi-value;
22
+ rb-clear-errinfo: func();
23
+
24
+ rstring-ptr: func(value: borrow<rb-abi-value>) -> string;
25
+
26
+ rb-vm-bugreport: func();
27
+
28
+ rb-gc-enable: func() -> bool;
29
+ rb-gc-disable: func() -> bool;
30
+
31
+ rb-set-should-prohibit-rewind: func(new-value: bool) -> bool;
32
+ // XXX: Do we really need them?
33
+ // wrap-js-value: func(value: js-abi-value) -> rb-abi-value;
34
+ // to-js-value: func(value: borrow<rb-abi-value>) -> js-abi-value;
35
+
36
+ // Transfer the value from Ruby to JS
37
+ //
38
+ // 1. Ruby side registers the value in the stage
39
+ // 2. Ruby side calls JS's `import-rb-value-from-rb()`
40
+ // 3. `import-rb-value-from-rb()` calls `export-rb-value-to-js()`
41
+ // 4. `export-rb-value-to-js()` returns the staged value
42
+ export-rb-value-to-js: func() -> rb-abi-value;
43
+ }
data/wit/world.wit ADDED
@@ -0,0 +1,6 @@
1
+ package ruby:js;
2
+
3
+ world ext {
4
+ import js-runtime;
5
+ export ruby-runtime;
6
+ }
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Saito
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-01-28 00:00:00.000000000 Z
10
+ date: 2024-05-04 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: JavaScript bindings for ruby.wasm. This gem provides a way to use JavaScript
14
13
  functionalities from Ruby through WebAssembly.
@@ -17,23 +16,23 @@ email:
17
16
  executables: []
18
17
  extensions:
19
18
  - ext/js/extconf.rb
20
- - ext/witapi/extconf.rb
21
19
  extra_rdoc_files: []
22
20
  files:
23
21
  - ext/js/bindgen/.clang-format
24
- - ext/js/bindgen/rb-js-abi-host.c
25
- - ext/js/bindgen/rb-js-abi-host.h
26
- - ext/js/bindgen/rb-js-abi-host.wit
22
+ - ext/js/bindgen/ext.c
23
+ - ext/js/bindgen/ext.h
24
+ - ext/js/bindgen/ext_component_type.o
25
+ - ext/js/bindgen/legacy/rb-abi-guest.c
26
+ - ext/js/bindgen/legacy/rb-abi-guest.h
27
+ - ext/js/bindgen/legacy/rb-abi-guest.wit
28
+ - ext/js/bindgen/legacy/rb-js-abi-host.c
29
+ - ext/js/bindgen/legacy/rb-js-abi-host.h
30
+ - ext/js/bindgen/legacy/rb-js-abi-host.wit
27
31
  - ext/js/depend
28
32
  - ext/js/extconf.rb
29
33
  - ext/js/js-core.c
30
- - ext/witapi/bindgen/.clang-format
31
- - ext/witapi/bindgen/rb-abi-guest.c
32
- - ext/witapi/bindgen/rb-abi-guest.h
33
- - ext/witapi/bindgen/rb-abi-guest.wit
34
- - ext/witapi/depend
35
- - ext/witapi/extconf.rb
36
- - ext/witapi/witapi-core.c
34
+ - ext/js/types.h
35
+ - ext/js/witapi-core.c
37
36
  - js.gemspec
38
37
  - lib/js.rb
39
38
  - lib/js/array.rb
@@ -43,12 +42,14 @@ files:
43
42
  - lib/js/require_remote/evaluator.rb
44
43
  - lib/js/require_remote/url_resolver.rb
45
44
  - lib/js/version.rb
45
+ - wit/js-runtime.wit
46
+ - wit/ruby-runtime.wit
47
+ - wit/world.wit
46
48
  homepage: https://github.com/ruby/ruby.wasm
47
49
  licenses:
48
50
  - MIT
49
51
  metadata:
50
52
  source_code_uri: https://github.com/ruby/ruby.wasm/tree/main/packages/gems/js
51
- post_install_message:
52
53
  rdoc_options: []
53
54
  require_paths:
54
55
  - lib
@@ -63,8 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
64
  - !ruby/object:Gem::Version
64
65
  version: '0'
65
66
  requirements: []
66
- rubygems_version: 3.5.3
67
- signing_key:
67
+ rubygems_version: 3.6.0.dev
68
68
  specification_version: 4
69
69
  summary: JavaScript bindings for ruby.wasm
70
70
  test_files: []
@@ -1,2 +0,0 @@
1
- DisableFormat: true
2
- SortIncludes: false
data/ext/witapi/depend DELETED
@@ -1,11 +0,0 @@
1
- link.filelist:
2
- echo $(foreach obj,$(OBJS),$(abspath $(obj))) > $@
3
- echo -mexec-model=reactor >> $@
4
-
5
- witapi.a: link.filelist
6
-
7
- witapi-core.o: $(srcdir)/bindgen/rb-abi-guest.h
8
-
9
- bindgen/%.o: $(srcdir)/bindgen/%.c
10
- @mkdir -p "$(@D)"
11
- $(CC) -c -I$(srcdir)/bindgen -o $@ $<
@@ -1,3 +0,0 @@
1
- require "mkmf"
2
- $objs = %w[witapi-core.o bindgen/rb-abi-guest.o]
3
- create_makefile("witapi")