quickjs 0.13.0 → 0.14.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 +4 -4
- data/README.md +32 -2
- data/Rakefile +7 -0
- data/ext/quickjsrb/extconf.rb +7 -0
- data/ext/quickjsrb/quickjsrb.c +188 -53
- data/ext/quickjsrb/quickjsrb.h +6 -4
- data/ext/quickjsrb/quickjsrb_crypto.c +71 -0
- data/ext/quickjsrb/quickjsrb_crypto.h +6 -0
- data/ext/quickjsrb/quickjsrb_crypto_subtle.c +1001 -0
- data/ext/quickjsrb/quickjsrb_crypto_subtle.h +6 -0
- data/ext/quickjsrb/vendor/polyfill-intl-en.min.js +3 -11
- data/ext/quickjsrb/vendor/polyfill-url.min.js +1 -0
- data/lib/quickjs/crypto_key.rb +15 -0
- data/lib/quickjs/subtle_crypto.rb +493 -0
- data/lib/quickjs/version.rb +1 -1
- data/lib/quickjs.rb +3 -0
- data/polyfills/check-licenses.mjs +72 -0
- data/polyfills/package-lock.json +183 -154
- data/polyfills/package.json +9 -8
- data/polyfills/rolldown.config.mjs +8 -0
- data/polyfills/src/url.js +1089 -0
- data/sig/quickjs.rbs +6 -1
- metadata +11 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d0430b7f91db5ab0332aaa70759b574b286fcbaef0ae68c1345536256b98e9a8
|
|
4
|
+
data.tar.gz: '088306bccab3579af65b1ffade1780141527b1dd562b9400203707fba2464b84'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e876fa5eb3f2937006c1fe1ceea5445b12928f15df06ccfedf644cd5ef5e97774dba79e08210620fa3d8ae4802da2d3d87c8c34340b0125705f330611077a2ae
|
|
7
|
+
data.tar.gz: 8e0ab490bf17410f9a648ba98b9fcce7f510a67e1fbbd0d7c383b560ef11d6911f2bea906474671c4f1bb874fe312c0758df2d508366208720dc4c11aff519e7
|
data/README.md
CHANGED
|
@@ -61,6 +61,8 @@ Quickjs.eval_code(code, features: [::Quickjs::MODULE_STD, ::Quickjs::POLYFILL_FI
|
|
|
61
61
|
| `POLYFILL_INTL` | Intl API (DateTimeFormat, NumberFormat, PluralRules, Locale) |
|
|
62
62
|
| `POLYFILL_FILE` | W3C File API (Blob and File) |
|
|
63
63
|
| `POLYFILL_ENCODING` | Encoding API (TextEncoder and TextDecoder) |
|
|
64
|
+
| `POLYFILL_URL` | URL API (URL and URLSearchParams) |
|
|
65
|
+
| `POLYFILL_CRYPTO` | Web Crypto API (`crypto.getRandomValues`, `crypto.randomUUID`, `crypto.subtle`); combine with `POLYFILL_ENCODING` for string↔buffer conversion |
|
|
64
66
|
|
|
65
67
|
</details>
|
|
66
68
|
|
|
@@ -76,6 +78,29 @@ vm.eval_code('a.b = "d";')
|
|
|
76
78
|
vm.eval_code('a.b;') #=> "d"
|
|
77
79
|
```
|
|
78
80
|
|
|
81
|
+
#### `Quickjs::VM#call`: ⚡ Call a JS function directly with Ruby arguments
|
|
82
|
+
|
|
83
|
+
```rb
|
|
84
|
+
vm = Quickjs::VM.new
|
|
85
|
+
vm.eval_code('function add(a, b) { return a + b; }')
|
|
86
|
+
|
|
87
|
+
vm.call('add', 1, 2) #=> 3
|
|
88
|
+
vm.call(:add, 1, 2) #=> 3 (Symbol also works)
|
|
89
|
+
|
|
90
|
+
# Nested functions — preserves `this` binding
|
|
91
|
+
vm.eval_code('const counter = { n: 0, inc() { return ++this.n; } }')
|
|
92
|
+
vm.call('counter.inc') #=> 1
|
|
93
|
+
vm.call('counter.inc') #=> 2
|
|
94
|
+
|
|
95
|
+
# Keys with special characters via bracket notation
|
|
96
|
+
vm.eval_code("const obj = {}; obj['my-fn'] = x => x * 2;")
|
|
97
|
+
vm.call('obj["my-fn"]', 21) #=> 42
|
|
98
|
+
|
|
99
|
+
# Async functions are automatically awaited
|
|
100
|
+
vm.eval_code('async function fetchVal() { return 42; }')
|
|
101
|
+
vm.call('fetchVal') #=> 42
|
|
102
|
+
```
|
|
103
|
+
|
|
79
104
|
#### `Quickjs::VM#import`: 🔌 Import ESM from a source code
|
|
80
105
|
|
|
81
106
|
```rb
|
|
@@ -165,13 +190,18 @@ vm.eval_code('console.log("hello", 42)')
|
|
|
165
190
|
- `ext/quickjsrb/quickjs`
|
|
166
191
|
- [MIT License Copyright (c) 2017-2021 by Fabrice Bellard and Charlie Gordon](https://github.com/bellard/quickjs/blob/6e2e68fd0896957f92eb6c242a2e048c1ef3cae0/LICENSE).
|
|
167
192
|
- `ext/quickjsrb/vendor/polyfill-intl-en.min.js` ([bundled and minified from `polyfills/`](https://github.com/hmsk/quickjs.rb/tree/main/polyfills))
|
|
193
|
+
- MIT License Copyright (c) 2022 FormatJS
|
|
194
|
+
- [@formatjs/intl-supportedvaluesof](https://github.com/formatjs/formatjs/blob/main/packages/intl-supportedvaluesof/LICENSE.md)
|
|
168
195
|
- MIT License Copyright (c) 2023 FormatJS
|
|
169
196
|
- [@formatjs/intl-getcanonicallocales](https://github.com/formatjs/formatjs/blob/main/packages/intl-getcanonicallocales/LICENSE.md)
|
|
170
197
|
- [@formatjs/intl-locale](https://github.com/formatjs/formatjs/blob/main/packages/intl-locale/LICENSE.md)
|
|
171
198
|
- [@formatjs/intl-pluralrules](https://github.com/formatjs/formatjs/blob/main/packages/intl-pluralrules/LICENSE.md)
|
|
172
199
|
- [@formatjs/intl-numberformat](https://github.com/formatjs/formatjs/blob/main/packages/intl-numberformat/LICENSE.md)
|
|
173
200
|
- [@formatjs/intl-datetimeformat](https://github.com/formatjs/formatjs/blob/main/packages/intl-datetimeformat/LICENSE.md)
|
|
174
|
-
|
|
175
|
-
- [
|
|
201
|
+
- [@formatjs/ecma402-abstract](https://github.com/formatjs/formatjs/blob/main/packages/ecma402-abstract/LICENSE.md)
|
|
202
|
+
- [@formatjs/fast-memoize](https://github.com/formatjs/formatjs/blob/main/packages/fast-memoize/LICENSE.md)
|
|
203
|
+
- [@formatjs/intl-localematcher](https://github.com/formatjs/formatjs/blob/main/packages/intl-localematcher/LICENSE.md)
|
|
204
|
+
- MIT License Copyright (c) 2026 FormatJS
|
|
205
|
+
- [@formatjs/bigdecimal](https://github.com/formatjs/formatjs/blob/main/packages/bigdecimal/LICENSE.md)
|
|
176
206
|
|
|
177
207
|
Otherwise, [the MIT License, Copyright 2024 by Kengo Hamasaki](/LICENSE).
|
data/Rakefile
CHANGED
|
@@ -54,6 +54,13 @@ namespace :polyfills do
|
|
|
54
54
|
Rake::Task[:compile].invoke
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
desc 'Check licenses of bundled polyfill dependencies'
|
|
58
|
+
task :check_licenses do
|
|
59
|
+
Dir.chdir(File.expand_path('polyfills', __dir__)) do
|
|
60
|
+
sh 'npm run check-licenses'
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
57
64
|
namespace :version do
|
|
58
65
|
task :check do
|
|
59
66
|
check_polyfill_version! do |pkg_v, gem_v|
|
data/ext/quickjsrb/extconf.rb
CHANGED
|
@@ -14,8 +14,11 @@ $srcs = [
|
|
|
14
14
|
'polyfill-intl-en.min.c',
|
|
15
15
|
'polyfill-file.min.c',
|
|
16
16
|
'polyfill-encoding.min.c',
|
|
17
|
+
'polyfill-url.min.c',
|
|
17
18
|
'quickjsrb.c',
|
|
18
19
|
'quickjsrb_file.c',
|
|
20
|
+
'quickjsrb_crypto.c',
|
|
21
|
+
'quickjsrb_crypto_subtle.c',
|
|
19
22
|
]
|
|
20
23
|
|
|
21
24
|
append_cflags('-I$(srcdir)/quickjs')
|
|
@@ -72,6 +75,10 @@ polyfill-encoding.min.js:
|
|
|
72
75
|
$(COPY) $(srcdir)/vendor/$@ $@
|
|
73
76
|
polyfill-encoding.min.c: ./qjsc polyfill-encoding.min.js
|
|
74
77
|
./qjsc -fno-string-normalize -fno-eval -fno-proxy -fno-module-loader -c -M polyfill/encoding.so,encoding -m -o $@ polyfill-encoding.min.js
|
|
78
|
+
polyfill-url.min.js:
|
|
79
|
+
$(COPY) $(srcdir)/vendor/$@ $@
|
|
80
|
+
polyfill-url.min.c: ./qjsc polyfill-url.min.js
|
|
81
|
+
./qjsc -fno-string-normalize -fno-eval -fno-proxy -fno-module-loader -c -M polyfill/url.so,url -m -o $@ polyfill-url.min.js
|
|
75
82
|
COMPILE_POLYFILL
|
|
76
83
|
conf
|
|
77
84
|
end
|
data/ext/quickjsrb/quickjsrb.c
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#include "quickjsrb.h"
|
|
2
2
|
#include "quickjsrb_file.h"
|
|
3
|
+
#include "quickjsrb_crypto.h"
|
|
3
4
|
|
|
4
5
|
const char *featureStdId = "feature_std";
|
|
5
6
|
const char *featureOsId = "feature_os";
|
|
@@ -7,6 +8,8 @@ const char *featureTimeoutId = "feature_timeout";
|
|
|
7
8
|
const char *featurePolyfillIntlId = "feature_polyfill_intl";
|
|
8
9
|
const char *featurePolyfillFileId = "feature_polyfill_file";
|
|
9
10
|
const char *featurePolyfillEncodingId = "feature_polyfill_encoding";
|
|
11
|
+
const char *featurePolyfillUrlId = "feature_polyfill_url";
|
|
12
|
+
const char *featurePolyfillCryptoId = "feature_polyfill_crypto";
|
|
10
13
|
|
|
11
14
|
const char *undefinedId = "undefined";
|
|
12
15
|
const char *nanId = "NaN";
|
|
@@ -175,17 +178,18 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
|
175
178
|
return JS_ToBool(ctx, j_val) > 0 ? Qtrue : Qfalse;
|
|
176
179
|
}
|
|
177
180
|
case JS_TAG_STRING:
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
181
|
+
case JS_TAG_STRING_ROPE:
|
|
182
|
+
{
|
|
183
|
+
// QuickJS keeps long `s += chunk` chains as a rope (JS_TAG_STRING_ROPE)
|
|
184
|
+
// until something materialises them. JS_ToCStringLen flattens ropes
|
|
185
|
+
// transparently, so both tags share the same conversion path.
|
|
186
|
+
size_t len;
|
|
187
|
+
const char *str = JS_ToCStringLen(ctx, &len, j_val);
|
|
188
|
+
if (str == NULL)
|
|
183
189
|
return Qnil;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return r_result;
|
|
188
|
-
}
|
|
190
|
+
VALUE r_str = rb_utf8_str_new(str, (long)len);
|
|
191
|
+
JS_FreeCString(ctx, str);
|
|
192
|
+
return r_str;
|
|
189
193
|
}
|
|
190
194
|
case JS_TAG_OBJECT:
|
|
191
195
|
{
|
|
@@ -486,20 +490,14 @@ static VALUE r_try_call_listener(VALUE r_args)
|
|
|
486
490
|
|
|
487
491
|
static int dispatch_log(VMData *data, const char *severity, VALUE r_row)
|
|
488
492
|
{
|
|
493
|
+
if (NIL_P(data->log_listener))
|
|
494
|
+
return 0;
|
|
495
|
+
|
|
489
496
|
VALUE r_log = r_log_new(severity, r_row);
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
rb_protect(r_try_call_listener, r_args, &error);
|
|
495
|
-
if (error)
|
|
496
|
-
return error;
|
|
497
|
-
}
|
|
498
|
-
else
|
|
499
|
-
{
|
|
500
|
-
rb_ary_push(data->logs, r_log);
|
|
501
|
-
}
|
|
502
|
-
return 0;
|
|
497
|
+
VALUE r_args = rb_ary_new3(2, data->log_listener, r_log);
|
|
498
|
+
int error;
|
|
499
|
+
rb_protect(r_try_call_listener, r_args, &error);
|
|
500
|
+
return error;
|
|
503
501
|
}
|
|
504
502
|
|
|
505
503
|
static VALUE vm_m_on_log(VALUE r_self)
|
|
@@ -680,6 +678,18 @@ static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
|
|
|
680
678
|
JS_FreeValue(data->context, j_polyfillEncodingResult);
|
|
681
679
|
}
|
|
682
680
|
|
|
681
|
+
if (RTEST(rb_funcall(r_features, rb_intern("include?"), 1, QUICKJSRB_SYM(featurePolyfillUrlId))))
|
|
682
|
+
{
|
|
683
|
+
JSValue j_polyfillUrlObject = JS_ReadObject(data->context, &qjsc_polyfill_url_min, qjsc_polyfill_url_min_size, JS_READ_OBJ_BYTECODE);
|
|
684
|
+
JSValue j_polyfillUrlResult = JS_EvalFunction(data->context, j_polyfillUrlObject);
|
|
685
|
+
JS_FreeValue(data->context, j_polyfillUrlResult);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (RTEST(rb_funcall(r_features, rb_intern("include?"), 1, QUICKJSRB_SYM(featurePolyfillCryptoId))))
|
|
689
|
+
{
|
|
690
|
+
quickjsrb_init_crypto(data->context, j_global);
|
|
691
|
+
}
|
|
692
|
+
|
|
683
693
|
JSValue j_console = JS_NewObject(data->context);
|
|
684
694
|
JS_SetPropertyStr(
|
|
685
695
|
data->context, j_console, "log",
|
|
@@ -713,6 +723,20 @@ static int interrupt_handler(JSRuntime *runtime, void *opaque)
|
|
|
713
723
|
return elapsed_ms >= eval_time->limit_ms ? 1 : 0;
|
|
714
724
|
}
|
|
715
725
|
|
|
726
|
+
static VALUE to_rb_return_value(JSContext *ctx, JSValue j_val)
|
|
727
|
+
{
|
|
728
|
+
if (JS_VALUE_GET_NORM_TAG(j_val) == JS_TAG_OBJECT && JS_PromiseState(ctx, j_val) != -1)
|
|
729
|
+
{
|
|
730
|
+
JS_FreeValue(ctx, j_val);
|
|
731
|
+
VALUE r_error_message = rb_str_new2("An unawaited Promise was returned to the top-level");
|
|
732
|
+
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_NO_AWAIT_ERROR), rb_intern("new"), 2, r_error_message, Qnil));
|
|
733
|
+
return Qnil;
|
|
734
|
+
}
|
|
735
|
+
VALUE result = to_rb_value(ctx, j_val);
|
|
736
|
+
JS_FreeValue(ctx, j_val);
|
|
737
|
+
return result;
|
|
738
|
+
}
|
|
739
|
+
|
|
716
740
|
static VALUE vm_m_evalCode(VALUE r_self, VALUE r_code)
|
|
717
741
|
{
|
|
718
742
|
VMData *data;
|
|
@@ -730,26 +754,11 @@ static VALUE vm_m_evalCode(VALUE r_self, VALUE r_code)
|
|
|
730
754
|
char *code = StringValueCStr(r_code);
|
|
731
755
|
JSValue j_codeResult = JS_Eval(data->context, code, strlen(code), "<code>", JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_ASYNC);
|
|
732
756
|
JSValue j_awaitedResult = js_std_await(data->context, j_codeResult); // This frees j_codeResult
|
|
757
|
+
// JS_EVAL_FLAG_ASYNC wraps the result in {value, done} — extract the actual value
|
|
758
|
+
// Free j_awaitedResult before to_rb_return_value because it may raise (longjmp), which would skip cleanup
|
|
733
759
|
JSValue j_returnedValue = JS_GetPropertyStr(data->context, j_awaitedResult, "value");
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
{
|
|
737
|
-
JS_FreeValue(data->context, j_returnedValue);
|
|
738
|
-
JS_FreeValue(data->context, j_awaitedResult);
|
|
739
|
-
VALUE r_error_message = rb_str_new2("An unawaited Promise was returned to the top-level");
|
|
740
|
-
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_NO_AWAIT_ERROR), rb_intern("new"), 2, r_error_message, Qnil));
|
|
741
|
-
return Qnil;
|
|
742
|
-
}
|
|
743
|
-
else
|
|
744
|
-
{
|
|
745
|
-
// Free j_awaitedResult before to_rb_value because to_rb_value may
|
|
746
|
-
// raise (longjmp) for JS exceptions, which would skip any cleanup
|
|
747
|
-
// after it and leak JS GC objects.
|
|
748
|
-
JS_FreeValue(data->context, j_awaitedResult);
|
|
749
|
-
VALUE result = to_rb_value(data->context, j_returnedValue);
|
|
750
|
-
JS_FreeValue(data->context, j_returnedValue);
|
|
751
|
-
return result;
|
|
752
|
-
}
|
|
760
|
+
JS_FreeValue(data->context, j_awaitedResult);
|
|
761
|
+
return to_rb_return_value(data->context, j_returnedValue);
|
|
753
762
|
}
|
|
754
763
|
|
|
755
764
|
static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
|
@@ -790,6 +799,142 @@ static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
|
|
790
799
|
return r_name_sym;
|
|
791
800
|
}
|
|
792
801
|
|
|
802
|
+
static VALUE vm_m_callGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
|
803
|
+
{
|
|
804
|
+
if (argc < 1)
|
|
805
|
+
rb_raise(rb_eArgError, "wrong number of arguments (given 0, expected 1+)");
|
|
806
|
+
|
|
807
|
+
VALUE r_name = argv[0];
|
|
808
|
+
|
|
809
|
+
VMData *data;
|
|
810
|
+
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
811
|
+
|
|
812
|
+
JSValue j_this = JS_UNDEFINED;
|
|
813
|
+
JSValue j_func;
|
|
814
|
+
|
|
815
|
+
VALUE r_path;
|
|
816
|
+
if (SYMBOL_P(r_name) || RB_TYPE_P(r_name, T_STRING))
|
|
817
|
+
{
|
|
818
|
+
VALUE r_name_str = rb_funcall(r_name, rb_intern("to_s"), 0);
|
|
819
|
+
const char *name_str = StringValueCStr(r_name_str);
|
|
820
|
+
size_t name_len = strlen(name_str);
|
|
821
|
+
const char *last_bracket = strrchr(name_str, '[');
|
|
822
|
+
const char *last_dot = strrchr(name_str, '.');
|
|
823
|
+
|
|
824
|
+
if (last_bracket != NULL && last_bracket != name_str && name_str[name_len - 1] == ']')
|
|
825
|
+
{
|
|
826
|
+
// Bracket notation: 'a["key"]' or 'a.b["key"]' or 'a[0]'
|
|
827
|
+
// Split into parent expression and the bracketed key
|
|
828
|
+
VALUE r_parent = rb_str_new(name_str, last_bracket - name_str);
|
|
829
|
+
const char *key_start = last_bracket + 1;
|
|
830
|
+
size_t key_len = name_len - (key_start - name_str) - 1; // exclude ']'
|
|
831
|
+
// Strip surrounding quotes for string keys: 'a["b"]' → key = b
|
|
832
|
+
if (key_len >= 2 &&
|
|
833
|
+
((key_start[0] == '\'' && key_start[key_len - 1] == '\'') ||
|
|
834
|
+
(key_start[0] == '"' && key_start[key_len - 1] == '"')))
|
|
835
|
+
{
|
|
836
|
+
key_start++;
|
|
837
|
+
key_len -= 2;
|
|
838
|
+
}
|
|
839
|
+
VALUE r_key = rb_str_new(key_start, key_len);
|
|
840
|
+
r_path = rb_ary_new3(2, r_parent, r_key);
|
|
841
|
+
}
|
|
842
|
+
else if (last_dot != NULL && last_dot != name_str)
|
|
843
|
+
{
|
|
844
|
+
// Dot notation: 'a.b.c' → ['a.b', 'c'] so the parent becomes `this`
|
|
845
|
+
VALUE r_parent = rb_str_new(name_str, last_dot - name_str);
|
|
846
|
+
VALUE r_key = rb_str_new2(last_dot + 1);
|
|
847
|
+
r_path = rb_ary_new3(2, r_parent, r_key);
|
|
848
|
+
}
|
|
849
|
+
else
|
|
850
|
+
{
|
|
851
|
+
r_path = rb_ary_new3(1, r_name_str);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
else
|
|
855
|
+
{
|
|
856
|
+
rb_raise(rb_eTypeError, "function's name should be a Symbol or a String");
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
{
|
|
860
|
+
long path_len = RARRAY_LEN(r_path);
|
|
861
|
+
|
|
862
|
+
VALUE r_first = RARRAY_AREF(r_path, 0);
|
|
863
|
+
if (!(SYMBOL_P(r_first) || RB_TYPE_P(r_first, T_STRING)))
|
|
864
|
+
rb_raise(rb_eTypeError, "function path elements should be Symbols or Strings");
|
|
865
|
+
|
|
866
|
+
VALUE r_first_str = rb_funcall(r_first, rb_intern("to_s"), 0);
|
|
867
|
+
const char *first_seg = StringValueCStr(r_first_str);
|
|
868
|
+
|
|
869
|
+
// JS_Eval accesses both global object properties and lexical (const/let) bindings
|
|
870
|
+
JSValue j_cur = JS_Eval(data->context, first_seg, strlen(first_seg), "<vm>", JS_EVAL_TYPE_GLOBAL);
|
|
871
|
+
if (JS_IsException(j_cur))
|
|
872
|
+
return to_rb_value(data->context, j_cur); // raises
|
|
873
|
+
|
|
874
|
+
for (long i = 1; i < path_len; i++)
|
|
875
|
+
{
|
|
876
|
+
VALUE r_seg = RARRAY_AREF(r_path, i);
|
|
877
|
+
if (!(SYMBOL_P(r_seg) || RB_TYPE_P(r_seg, T_STRING)))
|
|
878
|
+
{
|
|
879
|
+
JS_FreeValue(data->context, j_cur);
|
|
880
|
+
JS_FreeValue(data->context, j_this);
|
|
881
|
+
rb_raise(rb_eTypeError, "function path elements should be Symbols or Strings");
|
|
882
|
+
}
|
|
883
|
+
VALUE r_seg_str = rb_funcall(r_seg, rb_intern("to_s"), 0);
|
|
884
|
+
const char *seg = StringValueCStr(r_seg_str);
|
|
885
|
+
|
|
886
|
+
JSValue j_next = JS_GetPropertyStr(data->context, j_cur, seg);
|
|
887
|
+
if (JS_IsException(j_next))
|
|
888
|
+
{
|
|
889
|
+
JS_FreeValue(data->context, j_cur);
|
|
890
|
+
JS_FreeValue(data->context, j_this);
|
|
891
|
+
return to_rb_value(data->context, j_next); // raises
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
JS_FreeValue(data->context, j_this);
|
|
895
|
+
j_this = j_cur;
|
|
896
|
+
j_cur = j_next;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
j_func = j_cur;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
if (!JS_IsFunction(data->context, j_func))
|
|
903
|
+
{
|
|
904
|
+
JS_FreeValue(data->context, j_func);
|
|
905
|
+
JS_FreeValue(data->context, j_this);
|
|
906
|
+
VALUE r_error_message = rb_str_new2("given path is not a function");
|
|
907
|
+
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, r_error_message, Qnil));
|
|
908
|
+
return Qnil;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
int nargs = argc - 1;
|
|
912
|
+
JSValue *j_args = NULL;
|
|
913
|
+
if (nargs > 0)
|
|
914
|
+
{
|
|
915
|
+
j_args = (JSValue *)malloc(sizeof(JSValue) * nargs);
|
|
916
|
+
for (int i = 0; i < nargs; i++)
|
|
917
|
+
j_args[i] = to_js_value(data->context, argv[i + 1]);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
clock_gettime(CLOCK_MONOTONIC, &data->eval_time->started_at);
|
|
921
|
+
JS_SetInterruptHandler(JS_GetRuntime(data->context), interrupt_handler, data->eval_time);
|
|
922
|
+
|
|
923
|
+
JSValue j_result = JS_Call(data->context, j_func, j_this, nargs, (JSValueConst *)j_args);
|
|
924
|
+
|
|
925
|
+
JS_FreeValue(data->context, j_func);
|
|
926
|
+
JS_FreeValue(data->context, j_this);
|
|
927
|
+
if (j_args)
|
|
928
|
+
{
|
|
929
|
+
for (int i = 0; i < nargs; i++)
|
|
930
|
+
JS_FreeValue(data->context, j_args[i]);
|
|
931
|
+
free(j_args);
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
// js_std_await handles both async (promise) and sync results; frees j_result
|
|
935
|
+
return to_rb_return_value(data->context, js_std_await(data->context, j_result));
|
|
936
|
+
}
|
|
937
|
+
|
|
793
938
|
static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
|
|
794
939
|
{
|
|
795
940
|
VALUE r_import_string, r_opts;
|
|
@@ -850,16 +995,6 @@ static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
|
|
|
850
995
|
return Qtrue;
|
|
851
996
|
}
|
|
852
997
|
|
|
853
|
-
static VALUE vm_m_logs(VALUE r_self)
|
|
854
|
-
{
|
|
855
|
-
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "Quickjs::VM#logs is deprecated; use Quickjs::VM#on_log instead");
|
|
856
|
-
|
|
857
|
-
VMData *data;
|
|
858
|
-
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
|
859
|
-
|
|
860
|
-
return data->logs;
|
|
861
|
-
}
|
|
862
|
-
|
|
863
998
|
RUBY_FUNC_EXPORTED void Init_quickjsrb(void)
|
|
864
999
|
{
|
|
865
1000
|
rb_require("json");
|
|
@@ -873,9 +1008,9 @@ RUBY_FUNC_EXPORTED void Init_quickjsrb(void)
|
|
|
873
1008
|
rb_define_alloc_func(r_class_vm, vm_alloc);
|
|
874
1009
|
rb_define_method(r_class_vm, "initialize", vm_m_initialize, -1);
|
|
875
1010
|
rb_define_method(r_class_vm, "eval_code", vm_m_evalCode, 1);
|
|
1011
|
+
rb_define_method(r_class_vm, "call", vm_m_callGlobalFunction, -1);
|
|
876
1012
|
rb_define_method(r_class_vm, "define_function", vm_m_defineGlobalFunction, -1);
|
|
877
1013
|
rb_define_method(r_class_vm, "import", vm_m_import, -1);
|
|
878
|
-
rb_define_method(r_class_vm, "logs", vm_m_logs, 0);
|
|
879
1014
|
rb_define_method(r_class_vm, "on_log", vm_m_on_log, 0);
|
|
880
1015
|
r_define_log_class(r_class_vm);
|
|
881
1016
|
}
|
data/ext/quickjsrb/quickjsrb.h
CHANGED
|
@@ -18,6 +18,8 @@ extern const uint32_t qjsc_polyfill_file_min_size;
|
|
|
18
18
|
extern const uint8_t qjsc_polyfill_file_min;
|
|
19
19
|
extern const uint32_t qjsc_polyfill_encoding_min_size;
|
|
20
20
|
extern const uint8_t qjsc_polyfill_encoding_min;
|
|
21
|
+
extern const uint32_t qjsc_polyfill_url_min_size;
|
|
22
|
+
extern const uint8_t qjsc_polyfill_url_min;
|
|
21
23
|
|
|
22
24
|
extern const char *featureStdId;
|
|
23
25
|
extern const char *featureOsId;
|
|
@@ -25,6 +27,8 @@ extern const char *featureTimeoutId;
|
|
|
25
27
|
extern const char *featurePolyfillIntlId;
|
|
26
28
|
extern const char *featurePolyfillFileId;
|
|
27
29
|
extern const char *featurePolyfillEncodingId;
|
|
30
|
+
extern const char *featurePolyfillUrlId;
|
|
31
|
+
extern const char *featurePolyfillCryptoId;
|
|
28
32
|
|
|
29
33
|
extern const char *undefinedId;
|
|
30
34
|
extern const char *nanId;
|
|
@@ -48,7 +52,6 @@ typedef struct VMData
|
|
|
48
52
|
struct JSContext *context;
|
|
49
53
|
VALUE defined_functions;
|
|
50
54
|
struct EvalTime *eval_time;
|
|
51
|
-
VALUE logs;
|
|
52
55
|
VALUE log_listener;
|
|
53
56
|
VALUE alive_objects;
|
|
54
57
|
JSValue j_file_proxy_creator;
|
|
@@ -80,7 +83,6 @@ static void vm_mark(void *ptr)
|
|
|
80
83
|
{
|
|
81
84
|
VMData *data = (VMData *)ptr;
|
|
82
85
|
rb_gc_mark_movable(data->defined_functions);
|
|
83
|
-
rb_gc_mark_movable(data->logs);
|
|
84
86
|
rb_gc_mark_movable(data->log_listener);
|
|
85
87
|
rb_gc_mark_movable(data->alive_objects);
|
|
86
88
|
}
|
|
@@ -89,7 +91,6 @@ static void vm_compact(void *ptr)
|
|
|
89
91
|
{
|
|
90
92
|
VMData *data = (VMData *)ptr;
|
|
91
93
|
data->defined_functions = rb_gc_location(data->defined_functions);
|
|
92
|
-
data->logs = rb_gc_location(data->logs);
|
|
93
94
|
data->log_listener = rb_gc_location(data->log_listener);
|
|
94
95
|
data->alive_objects = rb_gc_location(data->alive_objects);
|
|
95
96
|
}
|
|
@@ -110,7 +111,6 @@ static VALUE vm_alloc(VALUE r_self)
|
|
|
110
111
|
VMData *data;
|
|
111
112
|
VALUE obj = TypedData_Make_Struct(r_self, VMData, &vm_type, data);
|
|
112
113
|
data->defined_functions = rb_hash_new();
|
|
113
|
-
data->logs = rb_ary_new();
|
|
114
114
|
data->log_listener = Qnil;
|
|
115
115
|
data->alive_objects = rb_hash_new();
|
|
116
116
|
data->j_file_proxy_creator = JS_UNDEFINED;
|
|
@@ -156,6 +156,8 @@ static void r_define_constants(VALUE r_parent_class)
|
|
|
156
156
|
rb_define_const(r_parent_class, "POLYFILL_INTL", QUICKJSRB_SYM(featurePolyfillIntlId));
|
|
157
157
|
rb_define_const(r_parent_class, "POLYFILL_FILE", QUICKJSRB_SYM(featurePolyfillFileId));
|
|
158
158
|
rb_define_const(r_parent_class, "POLYFILL_ENCODING", QUICKJSRB_SYM(featurePolyfillEncodingId));
|
|
159
|
+
rb_define_const(r_parent_class, "POLYFILL_URL", QUICKJSRB_SYM(featurePolyfillUrlId));
|
|
160
|
+
rb_define_const(r_parent_class, "POLYFILL_CRYPTO", QUICKJSRB_SYM(featurePolyfillCryptoId));
|
|
159
161
|
|
|
160
162
|
VALUE rb_cQuickjsValue = rb_define_class_under(r_parent_class, "Value", rb_cObject);
|
|
161
163
|
rb_define_const(rb_cQuickjsValue, "UNDEFINED", QUICKJSRB_SYM(undefinedId));
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#include "quickjsrb.h"
|
|
2
|
+
#include "quickjsrb_crypto.h"
|
|
3
|
+
#include "quickjsrb_crypto_subtle.h"
|
|
4
|
+
|
|
5
|
+
static VALUE r_secure_random()
|
|
6
|
+
{
|
|
7
|
+
return rb_const_get(rb_cObject, rb_intern("SecureRandom"));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static JSValue js_crypto_get_random_values(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
11
|
+
{
|
|
12
|
+
if (argc < 1)
|
|
13
|
+
return JS_ThrowTypeError(ctx, "Failed to execute 'getRandomValues': 1 argument required, but only 0 present.");
|
|
14
|
+
|
|
15
|
+
size_t byte_offset, byte_length, bytes_per_element;
|
|
16
|
+
JSValue j_buffer = JS_GetTypedArrayBuffer(ctx, argv[0], &byte_offset, &byte_length, &bytes_per_element);
|
|
17
|
+
if (JS_IsException(j_buffer))
|
|
18
|
+
return JS_EXCEPTION;
|
|
19
|
+
|
|
20
|
+
JSValue j_ctor = JS_GetPropertyStr(ctx, argv[0], "constructor");
|
|
21
|
+
JSValue j_name = JS_GetPropertyStr(ctx, j_ctor, "name");
|
|
22
|
+
const char *name = JS_ToCString(ctx, j_name);
|
|
23
|
+
int is_float = name && (strcmp(name, "Float16Array") == 0 ||
|
|
24
|
+
strcmp(name, "Float32Array") == 0 ||
|
|
25
|
+
strcmp(name, "Float64Array") == 0);
|
|
26
|
+
JS_FreeCString(ctx, name);
|
|
27
|
+
JS_FreeValue(ctx, j_name);
|
|
28
|
+
JS_FreeValue(ctx, j_ctor);
|
|
29
|
+
|
|
30
|
+
if (is_float)
|
|
31
|
+
{
|
|
32
|
+
JS_FreeValue(ctx, j_buffer);
|
|
33
|
+
return JS_ThrowTypeError(ctx, "Failed to execute 'getRandomValues': The provided ArrayBufferView value must not be of floating-point type.");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (byte_length > 65536)
|
|
37
|
+
{
|
|
38
|
+
JS_FreeValue(ctx, j_buffer);
|
|
39
|
+
return JS_ThrowRangeError(ctx, "Failed to execute 'getRandomValues': The ArrayBufferView's byte length exceeds the number of bytes of entropy available via this API (65536).");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
size_t buf_size;
|
|
43
|
+
uint8_t *buf = JS_GetArrayBuffer(ctx, &buf_size, j_buffer);
|
|
44
|
+
JS_FreeValue(ctx, j_buffer);
|
|
45
|
+
|
|
46
|
+
if (!buf)
|
|
47
|
+
return JS_ThrowTypeError(ctx, "Failed to execute 'getRandomValues': Failed to get ArrayBuffer.");
|
|
48
|
+
|
|
49
|
+
VALUE r_random_bytes = rb_funcall(r_secure_random(), rb_intern("random_bytes"), 1, SIZET2NUM(byte_length));
|
|
50
|
+
const uint8_t *random_buf = (const uint8_t *)RSTRING_PTR(r_random_bytes);
|
|
51
|
+
memcpy(buf + byte_offset, random_buf, byte_length);
|
|
52
|
+
|
|
53
|
+
return JS_DupValue(ctx, argv[0]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static JSValue js_crypto_random_uuid(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
57
|
+
{
|
|
58
|
+
VALUE r_uuid = rb_funcall(r_secure_random(), rb_intern("uuid"), 0);
|
|
59
|
+
return JS_NewString(ctx, StringValueCStr(r_uuid));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
void quickjsrb_init_crypto(JSContext *ctx, JSValue j_global)
|
|
63
|
+
{
|
|
64
|
+
JSValue j_crypto = JS_NewObject(ctx);
|
|
65
|
+
JS_SetPropertyStr(ctx, j_crypto, "getRandomValues",
|
|
66
|
+
JS_NewCFunction(ctx, js_crypto_get_random_values, "getRandomValues", 1));
|
|
67
|
+
JS_SetPropertyStr(ctx, j_crypto, "randomUUID",
|
|
68
|
+
JS_NewCFunction(ctx, js_crypto_random_uuid, "randomUUID", 0));
|
|
69
|
+
quickjsrb_init_crypto_subtle(ctx, j_crypto);
|
|
70
|
+
JS_SetPropertyStr(ctx, j_global, "crypto", j_crypto);
|
|
71
|
+
}
|