quickjs 0.7.1 → 0.8.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 460654c8f6d8174bf3f7025c0ae6e37a0bed766f3a8bdf57da5b9f5fc4ef8e0b
4
- data.tar.gz: 100eb2a30022baada73c5d83eb79675c19ba1df439da494278638917c3df16a2
3
+ metadata.gz: a7de66a3eb1ddd2b9ab50dcbf88f083e42466a8b91343a79af6f01b847d99d01
4
+ data.tar.gz: f0bab5aa5a676731737633e67c463572dc5b52f959bf9f897883f0034927d314
5
5
  SHA512:
6
- metadata.gz: 1527a557f744d35f9d79965d51c70f82cba82328351e1ec301ea66fd0293a7cd8bbe713766c470648774c6b589382d4e4357d89b01aa788a8c089e29f974f652
7
- data.tar.gz: d06c84aac9d3a440e885d769dfceb3849276c3b93c6d43e60788c8bc476416f61fef2898f1f94c90db44a4e0bc7e847c19c23366983555ee22138feeb9889a75
6
+ metadata.gz: 52f0a7a7419f0b7bb3cb1efb0f6b33442b7446d95cee7567dcb5e959c0ccdbf12ceaa02144e1d66730e411eba734bfd06923b1f2ce83b5d96611bb11c6e49964
7
+ data.tar.gz: 9f32de511d5dfcaff34dc2cf63970743d8d933286a617178e188bc2d56407159b694af92f7a5466c32a69a618a25f1c365d10a435051e7a05b588a750e9f5a7c
data/README.md CHANGED
@@ -58,8 +58,8 @@ vm = Quickjs.eval_code(features: [::Quickjs::MODULE_STD])
58
58
  # Enable `os` module by quickjs: https://bellard.org/quickjs/quickjs.html#os-module
59
59
  vm = Quickjs.eval_code(features: [::Quickjs::MODULE_OS])
60
60
 
61
- # Expose `os.setTimouet` and `os.clearTimeout` from `os` module
62
- vm = Quickjs.eval_code(features: [::Quickjs::FEATURES_TIMEOUT])
61
+ # Provide `setTimeout` managed by CRuby
62
+ vm = Quickjs.eval_code(features: [::Quickjs::FEATURE_TIMEOUT])
63
63
 
64
64
  # Inject the polyfill of Intl
65
65
  vm = Quickjs.eval_code(features: [::Quickjs::POLYFILL_INTL])
@@ -98,8 +98,8 @@ vm = Quickjs::VM.new(features: [::Quickjs::MODULE_STD])
98
98
  # Enable `os` module by quickjs: https://bellard.org/quickjs/quickjs.html#os-module
99
99
  vm = Quickjs::VM.new(features: [::Quickjs::MODULE_OS])
100
100
 
101
- # Expose `os.setTimouet` and `os.clearTimeout` from `os` module
102
- vm = Quickjs::VM.new(features: [::Quickjs::FEATURES_TIMEOUT])
101
+ # Provide `setTimeout` managed by CRuby
102
+ vm = Quickjs::VM.new(features: [::Quickjs::FEATURE_TIMEOUT])
103
103
 
104
104
  # Inject the polyfill of Intl
105
105
  vm = Quickjs::VM.new(features: [::Quickjs::POLYFILL_INTL])
@@ -311,7 +311,7 @@ static JSValue js_quickjsrb_call_global(JSContext *ctx, JSValueConst _this, int
311
311
  const char *funcName = JS_ToCString(ctx, func_data[0]);
312
312
 
313
313
  VMData *data = JS_GetContextOpaque(ctx);
314
- VALUE r_proc = rb_hash_aref(data->defined_functions, rb_str_new2(funcName));
314
+ VALUE r_proc = rb_hash_aref(data->defined_functions, ID2SYM(rb_intern(funcName)));
315
315
  // Shouldn't happen
316
316
  if (r_proc == Qnil)
317
317
  {
@@ -542,22 +542,7 @@ static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
542
542
  JSValue j_osEval = JS_Eval(data->context, enableOs, strlen(enableOs), "<vm>", JS_EVAL_TYPE_MODULE);
543
543
  JS_FreeValue(data->context, j_osEval);
544
544
  }
545
- else if (RTEST(rb_funcall(r_features, rb_intern("include?"), 1, QUICKJSRB_SYM(featureOsTimeoutId))))
546
- {
547
- char *filename = random_string();
548
- js_init_module_os(data->context, filename); // Better if this is limited just only for setTimeout and clearTimeout
549
- const char *enableTimeoutTemplate = "import * as _os from '%s';\n"
550
- "globalThis.setTimeout = _os.setTimeout;\n"
551
- "globalThis.clearTimeout = _os.clearTimeout;\n";
552
- int length = snprintf(NULL, 0, enableTimeoutTemplate, filename);
553
- char *enableTimeout = (char *)malloc(length + 1);
554
- snprintf(enableTimeout, length + 1, enableTimeoutTemplate, filename);
555
-
556
- JSValue j_timeoutEval = JS_Eval(data->context, enableTimeout, strlen(enableTimeout), "<vm>", JS_EVAL_TYPE_MODULE);
557
- free(enableTimeout);
558
- JS_FreeValue(data->context, j_timeoutEval);
559
- }
560
- else if (RTEST(rb_funcall(r_features, rb_intern("include?"), 1, QUICKJSRB_SYM(featureOsTimeoutBetaId))))
545
+ else if (RTEST(rb_funcall(r_features, rb_intern("include?"), 1, QUICKJSRB_SYM(featureTimeoutId))))
561
546
  {
562
547
  JS_SetPropertyStr(
563
548
  data->context, j_global, "setTimeout",
@@ -643,11 +628,19 @@ static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
643
628
  VALUE r_block;
644
629
  rb_scan_args(argc, argv, "10*&", &r_name, &r_flags, &r_block);
645
630
 
631
+ if (!(SYMBOL_P(r_name) || RB_TYPE_P(r_name, T_STRING)))
632
+ {
633
+ rb_raise(rb_eTypeError, "function's name should be a Symbol or a String");
634
+ }
635
+
646
636
  VMData *data;
647
637
  TypedData_Get_Struct(r_self, VMData, &vm_type, data);
648
638
 
649
- rb_hash_aset(data->defined_functions, r_name, r_block);
650
- char *funcName = StringValueCStr(r_name);
639
+ VALUE r_name_sym = rb_funcall(r_name, rb_intern("to_sym"), 0);
640
+
641
+ rb_hash_aset(data->defined_functions, r_name_sym, r_block);
642
+ VALUE r_name_str = rb_funcall(r_name, rb_intern("to_s"), 0);
643
+ char *funcName = StringValueCStr(r_name_str);
651
644
 
652
645
  JSValueConst ruby_data[2];
653
646
  ruby_data[0] = JS_NewString(data->context, funcName);
@@ -661,7 +654,7 @@ static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
661
654
  JS_FreeValue(data->context, ruby_data[0]);
662
655
  JS_FreeValue(data->context, ruby_data[1]);
663
656
 
664
- return rb_funcall(r_name, rb_intern("to_sym"), 0);
657
+ return r_name_sym;
665
658
  }
666
659
 
667
660
  static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
@@ -17,8 +17,7 @@ extern const uint8_t qjsc_polyfill_intl_en_min;
17
17
 
18
18
  const char *featureStdId = "feature_std";
19
19
  const char *featureOsId = "feature_os";
20
- const char *featureOsTimeoutId = "feature_os_timeout";
21
- const char *featureOsTimeoutBetaId = "feature_os_timeout_beta";
20
+ const char *featureTimeoutId = "feature_timeout";
22
21
  const char *featurePolyfillIntlId = "feature_polyfill_intl";
23
22
 
24
23
  const char *undefinedId = "undefined";
@@ -137,8 +136,7 @@ static void r_define_constants(VALUE r_parent_class)
137
136
  {
138
137
  rb_define_const(r_parent_class, "MODULE_STD", QUICKJSRB_SYM(featureStdId));
139
138
  rb_define_const(r_parent_class, "MODULE_OS", QUICKJSRB_SYM(featureOsId));
140
- rb_define_const(r_parent_class, "FEATURES_TIMEOUT", QUICKJSRB_SYM(featureOsTimeoutId));
141
- rb_define_const(r_parent_class, "FEATURES_TIMEOUT_BETA", QUICKJSRB_SYM(featureOsTimeoutBetaId));
139
+ rb_define_const(r_parent_class, "FEATURE_TIMEOUT", QUICKJSRB_SYM(featureTimeoutId));
142
140
  rb_define_const(r_parent_class, "POLYFILL_INTL", QUICKJSRB_SYM(featurePolyfillIntlId));
143
141
 
144
142
  VALUE rb_cQuickjsValue = rb_define_class_under(r_parent_class, "Value", rb_cObject);
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quickjs
4
- VERSION = "0.7.1"
4
+ VERSION = "0.8.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quickjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hmsk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-24 00:00:00.000000000 Z
11
+ date: 2025-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json