js 2.5.1 → 2.6.0

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,123 @@ 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
+ // Initialize VFS runtime if it's used
367
+ // NOTE: We don't use wasi-vfs for PIC build. Instead, we use
368
+ // Component Model-native wasi-virt.
369
+ # ifndef __PIC__
370
+ __attribute__((weak)) extern void __wasi_vfs_rt_init(void);
371
+ if (__wasi_vfs_rt_init) {
372
+ __wasi_vfs_rt_init();
373
+ }
374
+ # endif
375
+ }
376
+ }
377
+
378
+ // Exported Functions from `ruby:js/ruby-runtime`
379
+ void exports_ruby_js_ruby_runtime_ruby_show_version(void) {
380
+ __wasm_call_ctors_if_needed();
381
+ rb_abi_guest_ruby_show_version();
382
+ }
383
+ void exports_ruby_js_ruby_runtime_ruby_init(void) {
384
+ __wasm_call_ctors_if_needed();
385
+ rb_abi_guest_ruby_init();
386
+ }
387
+ void exports_ruby_js_ruby_runtime_ruby_sysinit(ext_list_string_t *args) {
388
+ __wasm_call_ctors_if_needed();
389
+ rb_abi_guest_ruby_sysinit(args);
390
+ }
391
+ exports_ruby_js_ruby_runtime_own_rb_iseq_t
392
+ exports_ruby_js_ruby_runtime_ruby_options(ext_list_string_t *args) {
393
+ __wasm_call_ctors_if_needed();
394
+ return rb_abi_guest_ruby_options(args);
395
+ }
396
+ void exports_ruby_js_ruby_runtime_ruby_script(ext_string_t *name) {
397
+ __wasm_call_ctors_if_needed();
398
+ rb_abi_guest_ruby_script(name);
399
+ }
400
+ void exports_ruby_js_ruby_runtime_ruby_init_loadpath(void) {
401
+ __wasm_call_ctors_if_needed();
402
+ rb_abi_guest_ruby_init_loadpath();
403
+ }
404
+ void exports_ruby_js_ruby_runtime_rb_eval_string_protect(
405
+ ext_string_t *str,
406
+ exports_ruby_js_ruby_runtime_tuple2_own_rb_abi_value_s32_t *ret) {
407
+ __wasm_call_ctors_if_needed();
408
+ rb_abi_guest_rb_eval_string_protect(str, ret);
409
+ }
410
+ void exports_ruby_js_ruby_runtime_rb_funcallv_protect(
411
+ exports_ruby_js_ruby_runtime_borrow_rb_abi_value_t recv,
412
+ exports_ruby_js_ruby_runtime_rb_id_t mid,
413
+ exports_ruby_js_ruby_runtime_list_borrow_rb_abi_value_t *args,
414
+ exports_ruby_js_ruby_runtime_tuple2_own_rb_abi_value_s32_t *ret) {
415
+ __wasm_call_ctors_if_needed();
416
+ rb_abi_guest_rb_funcallv_protect(recv, mid, args, ret);
417
+ }
418
+ exports_ruby_js_ruby_runtime_rb_id_t
419
+ exports_ruby_js_ruby_runtime_rb_intern(ext_string_t *name) {
420
+ __wasm_call_ctors_if_needed();
421
+ return rb_abi_guest_rb_intern(name);
422
+ }
423
+ exports_ruby_js_ruby_runtime_own_rb_abi_value_t
424
+ exports_ruby_js_ruby_runtime_rb_errinfo(void) {
425
+ __wasm_call_ctors_if_needed();
426
+ return rb_abi_guest_rb_errinfo();
427
+ }
428
+ void exports_ruby_js_ruby_runtime_rb_clear_errinfo(void) {
429
+ __wasm_call_ctors_if_needed();
430
+ rb_abi_guest_rb_clear_errinfo();
431
+ }
432
+ void exports_ruby_js_ruby_runtime_rstring_ptr(
433
+ exports_ruby_js_ruby_runtime_borrow_rb_abi_value_t value,
434
+ ext_string_t *ret) {
435
+ __wasm_call_ctors_if_needed();
436
+ rb_abi_guest_rstring_ptr(value, ret);
437
+ }
438
+ void exports_ruby_js_ruby_runtime_rb_vm_bugreport(void) {
439
+ __wasm_call_ctors_if_needed();
440
+ rb_abi_guest_rb_vm_bugreport();
441
+ }
442
+ bool exports_ruby_js_ruby_runtime_rb_gc_enable(void) {
443
+ __wasm_call_ctors_if_needed();
444
+ return rb_abi_guest_rb_gc_enable();
445
+ }
446
+ bool exports_ruby_js_ruby_runtime_rb_gc_disable(void) {
447
+ __wasm_call_ctors_if_needed();
448
+ return rb_abi_guest_rb_gc_disable();
449
+ }
450
+ bool exports_ruby_js_ruby_runtime_rb_set_should_prohibit_rewind(
451
+ bool new_value) {
452
+ __wasm_call_ctors_if_needed();
453
+ return rb_abi_guest_rb_set_should_prohibit_rewind(new_value);
454
+ }
455
+ exports_ruby_js_ruby_runtime_own_rb_abi_value_t
456
+ exports_ruby_js_ruby_runtime_export_rb_value_to_js(void) {
457
+ __wasm_call_ctors_if_needed();
458
+ return rb_abi_export_rb_value_to_js();
459
+ }
460
+ #endif
461
+
325
462
  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.1"
2
+ VERSION = "2.6.0"
3
3
  end
data/lib/js.rb CHANGED
@@ -173,7 +173,11 @@ class JS::Object
173
173
  if sym_str.end_with?("?")
174
174
  # When a JS method is called with a ? suffix, it is treated as a predicate method,
175
175
  # and the return value is converted to a Ruby boolean value automatically.
176
- self.call(sym_str[0..-2].to_sym, *args, &block) == JS::True
176
+ result = self.call(sym_str[0..-2].to_sym, *args, &block)
177
+
178
+ # Type coerce the result to boolean type
179
+ # to match the true/false determination in JavaScript's if statement.
180
+ JS.global.Boolean(result) == JS::True
177
181
  elsif self[sym].typeof == "function"
178
182
  self.call(sym, *args, &block)
179
183
  else
@@ -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.1
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Saito
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-04-21 00:00:00.000000000 Z
10
+ date: 2024-05-05 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.4.19
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
- $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$< -I$(srcdir)/bindgen
@@ -1,3 +0,0 @@
1
- require "mkmf"
2
- $objs = %w[witapi-core.o bindgen/rb-abi-guest.o]
3
- create_makefile("witapi")