quickjs 0.2.2 → 0.2.3
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/ext/quickjsrb/quickjsrb.c +54 -25
- data/ext/quickjsrb/quickjsrb.h +2 -0
- data/lib/quickjs/version.rb +1 -1
- data/lib/quickjs.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f57f31bd8921f897f9d1c81d1c50d24774bbfba5d31a99528b138e82d3fad58
|
4
|
+
data.tar.gz: 28216a7b1711fe08ad3e2a5b021abebaadd2b5d3808584ff259d0129abf0adf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 355b53297b3c27269b71dca93e057ecfe02eac70755b5bd336b212092fb6da3c14ff3b2dd2f77214a62891b28d860e33f961329374ddef485e168dea41b50097
|
7
|
+
data.tar.gz: 7ff16986a8ef0efeff790389805fa6d36f6b03b5983e8f8131ff33f8369514344cc9f9b9f103f036ad305441e06dfdfdacd125bdb8b061967337235d4913c525
|
data/ext/quickjsrb/quickjsrb.c
CHANGED
@@ -165,6 +165,11 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
165
165
|
r_error_class = QUICKJSRB_ERROR_FOR(QUICKJSRB_INTERRUPTED_ERROR);
|
166
166
|
r_error_message = rb_str_new2("Code evaluation is interrupted by the timeout or something");
|
167
167
|
}
|
168
|
+
else if (strcmp(errorClassName, "InternalError") == 0 && strstr(errorClassMessage, "Ruby") != NULL)
|
169
|
+
{
|
170
|
+
r_error_class = QUICKJSRB_ERROR_FOR(QUICKJSRB_RUBY_FUNCTION_ERROR);
|
171
|
+
}
|
172
|
+
|
168
173
|
else if (strcmp(errorClassName, "Quickjs::InterruptedError") == 0)
|
169
174
|
{
|
170
175
|
r_error_class = QUICKJSRB_ERROR_FOR(QUICKJSRB_INTERRUPTED_ERROR);
|
@@ -211,6 +216,18 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
211
216
|
}
|
212
217
|
}
|
213
218
|
|
219
|
+
static VALUE r_try_call_proc(VALUE r_try_args)
|
220
|
+
{
|
221
|
+
return rb_funcall(
|
222
|
+
rb_const_get(rb_cClass, rb_intern("Quickjs")),
|
223
|
+
rb_intern("_with_timeout"),
|
224
|
+
3,
|
225
|
+
RARRAY_AREF(r_try_args, 2), // timeout
|
226
|
+
RARRAY_AREF(r_try_args, 0), // proc
|
227
|
+
RARRAY_AREF(r_try_args, 1) // args for proc
|
228
|
+
);
|
229
|
+
}
|
230
|
+
|
214
231
|
static JSValue js_quickjsrb_call_global(JSContext *ctx, JSValueConst _this, int _argc, JSValueConst *argv)
|
215
232
|
{
|
216
233
|
VMData *data = JS_GetContextOpaque(ctx);
|
@@ -223,17 +240,25 @@ static JSValue js_quickjsrb_call_global(JSContext *ctx, JSValueConst _this, int
|
|
223
240
|
{ // Shouldn't happen
|
224
241
|
return JS_ThrowReferenceError(ctx, "Proc `%s` is not defined", funcName);
|
225
242
|
}
|
226
|
-
JS_FreeCString(ctx, funcName);
|
227
243
|
|
228
|
-
VALUE
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
ULONG2NUM(data->eval_time->limit * 1000 / CLOCKS_PER_SEC),
|
233
|
-
r_proc,
|
234
|
-
to_rb_value(ctx, argv[1]));
|
244
|
+
VALUE r_call_args = rb_ary_new();
|
245
|
+
rb_ary_push(r_call_args, r_proc);
|
246
|
+
rb_ary_push(r_call_args, to_rb_value(ctx, argv[1]));
|
247
|
+
rb_ary_push(r_call_args, ULONG2NUM(data->eval_time->limit * 1000 / CLOCKS_PER_SEC));
|
235
248
|
|
236
|
-
|
249
|
+
int sadnessHappened;
|
250
|
+
VALUE r_result = rb_protect(r_try_call_proc, r_call_args, &sadnessHappened);
|
251
|
+
JSValue j_result;
|
252
|
+
if (sadnessHappened)
|
253
|
+
{
|
254
|
+
j_result = JS_ThrowInternalError(ctx, "unintentional error is raised while executing the function by Ruby: `%s`", funcName);
|
255
|
+
}
|
256
|
+
else
|
257
|
+
{
|
258
|
+
j_result = to_js_value(ctx, r_result);
|
259
|
+
}
|
260
|
+
JS_FreeCString(ctx, funcName);
|
261
|
+
return j_result;
|
237
262
|
}
|
238
263
|
|
239
264
|
static JSValue js_quickjsrb_log(JSContext *ctx, JSValueConst _this, int _argc, JSValueConst *argv)
|
@@ -412,30 +437,34 @@ static VALUE vm_m_evalCode(VALUE r_self, VALUE r_code)
|
|
412
437
|
}
|
413
438
|
}
|
414
439
|
|
415
|
-
static VALUE vm_m_defineGlobalFunction(VALUE
|
440
|
+
static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
416
441
|
{
|
417
442
|
rb_need_block();
|
418
443
|
|
444
|
+
VALUE r_name;
|
445
|
+
VALUE r_flags;
|
446
|
+
VALUE r_block;
|
447
|
+
rb_scan_args(argc, argv, "10*&", &r_name, &r_flags, &r_block);
|
448
|
+
|
449
|
+
const char *asyncKeyword =
|
450
|
+
RTEST(rb_funcall(r_flags, rb_intern("include?"), 1, ID2SYM(rb_intern("async")))) ? "async " : "";
|
451
|
+
|
419
452
|
VMData *data;
|
420
453
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
421
454
|
|
422
|
-
|
423
|
-
|
424
|
-
VALUE r_proc = rb_block_proc();
|
425
|
-
rb_hash_aset(data->defined_functions, r_name, r_proc);
|
426
|
-
char *funcName = StringValueCStr(r_name);
|
455
|
+
rb_hash_aset(data->defined_functions, r_name, r_block);
|
456
|
+
char *funcName = StringValueCStr(r_name);
|
427
457
|
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
458
|
+
const char *template = "globalThis['%s'] = %s(...args) => __quickjsrb.runRubyMethod('%s', args);\n";
|
459
|
+
int length = snprintf(NULL, 0, template, funcName, asyncKeyword, funcName);
|
460
|
+
char *result = (char *)malloc(length + 1);
|
461
|
+
snprintf(result, length + 1, template, funcName, asyncKeyword, funcName);
|
432
462
|
|
433
|
-
|
463
|
+
JSValue j_codeResult = JS_Eval(data->context, result, strlen(result), "<vm>", JS_EVAL_TYPE_MODULE);
|
434
464
|
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
}
|
465
|
+
free(result);
|
466
|
+
JS_FreeValue(data->context, j_codeResult);
|
467
|
+
return rb_funcall(r_name, rb_intern("to_sym"), 0, NULL);
|
439
468
|
|
440
469
|
return Qnil;
|
441
470
|
}
|
@@ -507,7 +536,7 @@ RUBY_FUNC_EXPORTED void Init_quickjsrb(void)
|
|
507
536
|
rb_define_alloc_func(r_class_vm, vm_alloc);
|
508
537
|
rb_define_method(r_class_vm, "initialize", vm_m_initialize, -1);
|
509
538
|
rb_define_method(r_class_vm, "eval_code", vm_m_evalCode, 1);
|
510
|
-
rb_define_method(r_class_vm, "define_function", vm_m_defineGlobalFunction, 1);
|
539
|
+
rb_define_method(r_class_vm, "define_function", vm_m_defineGlobalFunction, -1);
|
511
540
|
rb_define_method(r_class_vm, "import", vm_m_import, -1);
|
512
541
|
rb_define_method(r_class_vm, "logs", vm_m_logs, 0);
|
513
542
|
r_define_log_class(r_class_vm);
|
data/ext/quickjsrb/quickjsrb.h
CHANGED
@@ -183,6 +183,7 @@ static VALUE r_define_log_class(VALUE r_parent_class)
|
|
183
183
|
#define QUICKJSRB_ROOT_RUNTIME_ERROR "RuntimeError"
|
184
184
|
#define QUICKJSRB_INTERRUPTED_ERROR "InterruptedError"
|
185
185
|
#define QUICKJSRB_NO_AWAIT_ERROR "NoAwaitError"
|
186
|
+
#define QUICKJSRB_RUBY_FUNCTION_ERROR "RubyFunctionError"
|
186
187
|
|
187
188
|
#define QUICKJSRB_ERROR_FOR(name) \
|
188
189
|
(VALUE) { rb_const_get(rb_const_get(rb_cClass, rb_intern("Quickjs")), rb_intern(name)) }
|
@@ -211,6 +212,7 @@ static void r_define_exception_classes(VALUE r_parent_class)
|
|
211
212
|
// quickjsrb specific errors
|
212
213
|
rb_define_class_under(r_parent_class, QUICKJSRB_INTERRUPTED_ERROR, r_runtime_error);
|
213
214
|
rb_define_class_under(r_parent_class, QUICKJSRB_NO_AWAIT_ERROR, r_runtime_error);
|
215
|
+
rb_define_class_under(r_parent_class, QUICKJSRB_RUBY_FUNCTION_ERROR, r_runtime_error);
|
214
216
|
}
|
215
217
|
|
216
218
|
#endif /* QUICKJSRB_H */
|
data/lib/quickjs/version.rb
CHANGED
data/lib/quickjs.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hmsk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|