quickjs 0.4.0 → 0.5.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.
- checksums.yaml +4 -4
- data/ext/quickjsrb/quickjsrb.c +110 -99
- data/ext/quickjsrb/quickjsrb.h +17 -0
- data/lib/quickjs/version.rb +1 -1
- 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: c3241377c00ffedb82e1752917d2a3bd6ac1fb667eebc30eb16b153c83119103
|
4
|
+
data.tar.gz: adc59082d4d1eb6cfbe662b88d3cbbee6deeb933c623964ccb139f1ae5b238c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b434d7100850f01b027d437800018a14eec3d97292d3553736baf43ede5714146eb56c81b060462bc6e49a21df90aa562638ee9dfe35c73fdd8d0046f2eb65e0
|
7
|
+
data.tar.gz: 7b37256b42f7d5a767cb8a048ae0d287dc8d7c6590bb2155d059f8a5fc674d08fda84d1242d7bcdc198367e0aef5d4699cab6ecb984dc28e430571da25fc8122
|
data/ext/quickjsrb/quickjsrb.c
CHANGED
@@ -221,16 +221,8 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
221
221
|
snprintf(headline, length + 1, headlineTemplate, errorClassName, errorClassMessage, stackTrace);
|
222
222
|
|
223
223
|
VMData *data = JS_GetContextOpaque(ctx);
|
224
|
-
VALUE
|
225
|
-
|
226
|
-
rb_iv_set(r_log, "@severity", ID2SYM(rb_intern("error")));
|
227
|
-
VALUE r_row = rb_ary_new();
|
228
|
-
VALUE r_loghash = rb_hash_new();
|
229
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("raw")), rb_str_new2(headline));
|
230
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("c")), rb_str_new2(headline));
|
231
|
-
rb_ary_push(r_row, r_loghash);
|
232
|
-
rb_iv_set(r_log, "@row", r_row);
|
233
|
-
rb_ary_push(data->logs, r_log);
|
224
|
+
VALUE r_headline = rb_str_new2(headline);
|
225
|
+
rb_ary_push(data->logs, r_log_new("error", rb_ary_new3(1, r_log_body_new(r_headline, r_headline))));
|
234
226
|
|
235
227
|
JS_FreeValue(ctx, j_errorClassMessage);
|
236
228
|
JS_FreeValue(ctx, j_errorClassName);
|
@@ -271,16 +263,8 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
271
263
|
snprintf(headline, length + 1, headlineTemplate, errorMessage);
|
272
264
|
|
273
265
|
VMData *data = JS_GetContextOpaque(ctx);
|
274
|
-
VALUE
|
275
|
-
|
276
|
-
rb_iv_set(r_log, "@severity", ID2SYM(rb_intern("error")));
|
277
|
-
VALUE r_row = rb_ary_new();
|
278
|
-
VALUE r_loghash = rb_hash_new();
|
279
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("raw")), rb_str_new2(headline));
|
280
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("c")), rb_str_new2(headline));
|
281
|
-
rb_ary_push(r_row, r_loghash);
|
282
|
-
rb_iv_set(r_log, "@row", r_row);
|
283
|
-
rb_ary_push(data->logs, r_log);
|
266
|
+
VALUE r_headline = rb_str_new2(headline);
|
267
|
+
rb_ary_push(data->logs, r_log_new("error", rb_ary_new3(1, r_log_body_new(r_headline, r_headline))));
|
284
268
|
|
285
269
|
free(headline);
|
286
270
|
|
@@ -324,84 +308,117 @@ static VALUE r_try_call_proc(VALUE r_try_args)
|
|
324
308
|
);
|
325
309
|
}
|
326
310
|
|
327
|
-
static JSValue js_quickjsrb_call_global(JSContext *ctx, JSValueConst _this, int
|
311
|
+
static JSValue js_quickjsrb_call_global(JSContext *ctx, JSValueConst _this, int argc, JSValueConst *argv, int _magic, JSValue *func_data)
|
328
312
|
{
|
329
|
-
|
330
|
-
JSValue j_maybeFuncName = JS_ToString(ctx, argv[0]);
|
331
|
-
const char *funcName = JS_ToCString(ctx, j_maybeFuncName);
|
332
|
-
JS_FreeValue(ctx, j_maybeFuncName);
|
313
|
+
const char *funcName = JS_ToCString(ctx, func_data[0]);
|
333
314
|
|
315
|
+
VMData *data = JS_GetContextOpaque(ctx);
|
334
316
|
VALUE r_proc = rb_hash_aref(data->defined_functions, rb_str_new2(funcName));
|
335
317
|
if (r_proc == Qnil)
|
336
|
-
{
|
337
|
-
return JS_ThrowReferenceError(ctx, "Proc `%s` is not defined", funcName);
|
318
|
+
{ // Shouldn't happen
|
319
|
+
return JS_ThrowReferenceError(ctx, "Proc `%s` is not defined", funcName); // TODO: Free funcnName
|
338
320
|
}
|
321
|
+
JS_FreeCString(ctx, funcName);
|
339
322
|
|
340
323
|
VALUE r_call_args = rb_ary_new();
|
341
324
|
rb_ary_push(r_call_args, r_proc);
|
342
|
-
|
325
|
+
|
326
|
+
VALUE r_argv = rb_ary_new();
|
327
|
+
for (int i = 0; i < argc; i++)
|
328
|
+
{
|
329
|
+
rb_ary_push(r_argv, to_rb_value(ctx, argv[i]));
|
330
|
+
}
|
331
|
+
rb_ary_push(r_call_args, r_argv);
|
343
332
|
rb_ary_push(r_call_args, ULONG2NUM(data->eval_time->limit * 1000 / CLOCKS_PER_SEC));
|
344
333
|
|
345
334
|
int sadnessHappened;
|
346
|
-
|
347
|
-
|
348
|
-
if (sadnessHappened)
|
335
|
+
|
336
|
+
if (JS_ToBool(ctx, func_data[1]))
|
349
337
|
{
|
350
|
-
|
351
|
-
JSValue
|
352
|
-
|
338
|
+
JSValue promise, resolving_funcs[2];
|
339
|
+
JSValue ret_val;
|
340
|
+
|
341
|
+
promise = JS_NewPromiseCapability(ctx, resolving_funcs);
|
342
|
+
if (JS_IsException(promise))
|
343
|
+
return JS_EXCEPTION;
|
344
|
+
|
345
|
+
// Currently, it's blocking process but should be asynchronized
|
346
|
+
JSValue j_result;
|
347
|
+
VALUE r_result = rb_protect(r_try_call_proc, r_call_args, &sadnessHappened);
|
348
|
+
if (sadnessHappened)
|
349
|
+
{
|
350
|
+
VALUE r_error = rb_errinfo();
|
351
|
+
j_result = j_error_from_ruby_error(ctx, r_error);
|
352
|
+
ret_val = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED,
|
353
|
+
1, (JSValueConst *)&j_result);
|
354
|
+
}
|
355
|
+
else
|
356
|
+
{
|
357
|
+
j_result = to_js_value(ctx, r_result);
|
358
|
+
ret_val = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED,
|
359
|
+
1, (JSValueConst *)&j_result);
|
360
|
+
}
|
361
|
+
JS_FreeValue(ctx, j_result);
|
362
|
+
JS_FreeValue(ctx, ret_val);
|
363
|
+
JS_FreeValue(ctx, resolving_funcs[0]);
|
364
|
+
JS_FreeValue(ctx, resolving_funcs[1]);
|
365
|
+
return promise;
|
353
366
|
}
|
354
367
|
else
|
355
368
|
{
|
356
|
-
|
369
|
+
VALUE r_result = rb_protect(r_try_call_proc, r_call_args, &sadnessHappened);
|
370
|
+
if (sadnessHappened)
|
371
|
+
{
|
372
|
+
VALUE r_error = rb_errinfo();
|
373
|
+
JSValue j_error = j_error_from_ruby_error(ctx, r_error);
|
374
|
+
return JS_Throw(ctx, j_error);
|
375
|
+
}
|
376
|
+
else
|
377
|
+
{
|
378
|
+
return to_js_value(ctx, r_result);
|
379
|
+
}
|
357
380
|
}
|
358
|
-
JS_FreeCString(ctx, funcName);
|
359
|
-
return j_result;
|
360
381
|
}
|
361
382
|
|
362
|
-
static JSValue js_quickjsrb_log(JSContext *ctx, JSValueConst _this, int
|
383
|
+
static JSValue js_quickjsrb_log(JSContext *ctx, JSValueConst _this, int argc, JSValueConst *argv, const char *severity)
|
363
384
|
{
|
364
385
|
VMData *data = JS_GetContextOpaque(ctx);
|
365
|
-
JSValue j_severity = JS_ToString(ctx, argv[0]);
|
366
|
-
const char *severity = JS_ToCString(ctx, j_severity);
|
367
|
-
JS_FreeValue(ctx, j_severity);
|
368
|
-
|
369
|
-
VALUE r_log_class = rb_const_get(rb_const_get(rb_const_get(rb_cClass, rb_intern("Quickjs")), rb_intern("VM")), rb_intern("Log"));
|
370
|
-
VALUE r_log = rb_funcall(r_log_class, rb_intern("new"), 0);
|
371
|
-
rb_iv_set(r_log, "@severity", ID2SYM(rb_intern(severity)));
|
372
|
-
JS_FreeCString(ctx, severity);
|
373
|
-
|
374
386
|
VALUE r_row = rb_ary_new();
|
375
|
-
int i;
|
376
|
-
JSValue j_length = JS_GetPropertyStr(ctx, argv[1], "length");
|
377
|
-
int count;
|
378
|
-
JS_ToInt32(ctx, &count, j_length);
|
379
|
-
JS_FreeValue(ctx, j_length);
|
380
|
-
|
381
|
-
for (i = 0; i < count; i++)
|
387
|
+
for (int i = 0; i < argc; i++)
|
382
388
|
{
|
383
|
-
|
384
|
-
|
385
|
-
if (JS_VALUE_GET_NORM_TAG(j_logged) == JS_TAG_OBJECT && JS_PromiseState(ctx, j_logged) != -1)
|
386
|
-
{
|
387
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("raw")), rb_str_new2("Promise"));
|
388
|
-
}
|
389
|
-
else
|
390
|
-
{
|
391
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("raw")), to_rb_value(ctx, j_logged));
|
392
|
-
}
|
389
|
+
JSValue j_logged = argv[i];
|
390
|
+
VALUE r_raw = JS_VALUE_GET_NORM_TAG(j_logged) == JS_TAG_OBJECT && JS_PromiseState(ctx, j_logged) != -1 ? rb_str_new2("Promise") : to_rb_value(ctx, j_logged);
|
393
391
|
const char *body = JS_ToCString(ctx, j_logged);
|
394
|
-
|
395
|
-
rb_hash_aset(r_loghash, ID2SYM(rb_intern("c")), rb_str_new2(body));
|
392
|
+
VALUE r_c = rb_str_new2(body);
|
396
393
|
JS_FreeCString(ctx, body);
|
397
|
-
|
394
|
+
|
395
|
+
rb_ary_push(r_row, r_log_body_new(r_raw, r_c));
|
398
396
|
}
|
399
397
|
|
400
|
-
|
401
|
-
rb_ary_push(data->logs, r_log);
|
398
|
+
rb_ary_push(data->logs, r_log_new(severity, r_row));
|
402
399
|
return JS_UNDEFINED;
|
403
400
|
}
|
404
401
|
|
402
|
+
static JSValue js_console_info(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
|
403
|
+
{
|
404
|
+
return js_quickjsrb_log(ctx, this, argc, argv, "info");
|
405
|
+
}
|
406
|
+
|
407
|
+
static JSValue js_console_verbose(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
|
408
|
+
{
|
409
|
+
return js_quickjsrb_log(ctx, this, argc, argv, "verbose");
|
410
|
+
}
|
411
|
+
|
412
|
+
static JSValue js_console_warn(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
|
413
|
+
{
|
414
|
+
return js_quickjsrb_log(ctx, this, argc, argv, "warning");
|
415
|
+
}
|
416
|
+
|
417
|
+
static JSValue js_console_error(JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv)
|
418
|
+
{
|
419
|
+
return js_quickjsrb_log(ctx, this, argc, argv, "error");
|
420
|
+
}
|
421
|
+
|
405
422
|
static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
|
406
423
|
{
|
407
424
|
VALUE r_opts;
|
@@ -473,29 +490,27 @@ static VALUE vm_m_initialize(int argc, VALUE *argv, VALUE r_self)
|
|
473
490
|
JS_FreeValue(data->context, j_timeoutEval);
|
474
491
|
}
|
475
492
|
|
476
|
-
JSValue j_global = JS_GetGlobalObject(data->context);
|
477
|
-
JSValue j_quickjsrbGlobal = JS_NewObject(data->context);
|
478
|
-
JS_SetPropertyStr(
|
479
|
-
data->context, j_quickjsrbGlobal, "runRubyMethod",
|
480
|
-
JS_NewCFunction(data->context, js_quickjsrb_call_global, "runRubyMethod", 2));
|
481
|
-
|
482
|
-
JS_SetPropertyStr(data->context, j_global, "__quickjsrb", j_quickjsrbGlobal);
|
483
|
-
|
484
493
|
JSValue j_console = JS_NewObject(data->context);
|
485
494
|
JS_SetPropertyStr(
|
486
|
-
data->context,
|
487
|
-
JS_NewCFunction(data->context,
|
495
|
+
data->context, j_console, "log",
|
496
|
+
JS_NewCFunction(data->context, js_console_info, "log", 1));
|
497
|
+
JS_SetPropertyStr(
|
498
|
+
data->context, j_console, "debug",
|
499
|
+
JS_NewCFunction(data->context, js_console_verbose, "debug", 1));
|
500
|
+
JS_SetPropertyStr(
|
501
|
+
data->context, j_console, "info",
|
502
|
+
JS_NewCFunction(data->context, js_console_info, "info", 1));
|
503
|
+
JS_SetPropertyStr(
|
504
|
+
data->context, j_console, "warn",
|
505
|
+
JS_NewCFunction(data->context, js_console_warn, "warn", 1));
|
506
|
+
JS_SetPropertyStr(
|
507
|
+
data->context, j_console, "error",
|
508
|
+
JS_NewCFunction(data->context, js_console_error, "error", 1));
|
509
|
+
|
510
|
+
JSValue j_global = JS_GetGlobalObject(data->context);
|
488
511
|
JS_SetPropertyStr(data->context, j_global, "console", j_console);
|
489
512
|
JS_FreeValue(data->context, j_global);
|
490
513
|
|
491
|
-
const char *defineLoggers = "console.log = (...args) => __quickjsrb.log('info', args);\n"
|
492
|
-
"console.debug = (...args) => __quickjsrb.log('verbose', args);\n"
|
493
|
-
"console.info = (...args) => __quickjsrb.log('info', args);\n"
|
494
|
-
"console.warn = (...args) => __quickjsrb.log('warning', args);\n"
|
495
|
-
"console.error = (...args) => __quickjsrb.log('error', args);\n";
|
496
|
-
JSValue j_defineLoggers = JS_Eval(data->context, defineLoggers, strlen(defineLoggers), "<vm>", JS_EVAL_TYPE_GLOBAL);
|
497
|
-
JS_FreeValue(data->context, j_defineLoggers);
|
498
|
-
|
499
514
|
return r_self;
|
500
515
|
}
|
501
516
|
|
@@ -544,27 +559,23 @@ static VALUE vm_m_defineGlobalFunction(int argc, VALUE *argv, VALUE r_self)
|
|
544
559
|
VALUE r_block;
|
545
560
|
rb_scan_args(argc, argv, "10*&", &r_name, &r_flags, &r_block);
|
546
561
|
|
547
|
-
const char *asyncKeyword =
|
548
|
-
RTEST(rb_funcall(r_flags, rb_intern("include?"), 1, ID2SYM(rb_intern("async")))) ? "async " : "";
|
549
|
-
|
550
562
|
VMData *data;
|
551
563
|
TypedData_Get_Struct(r_self, VMData, &vm_type, data);
|
552
564
|
|
553
565
|
rb_hash_aset(data->defined_functions, r_name, r_block);
|
554
566
|
char *funcName = StringValueCStr(r_name);
|
555
567
|
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
snprintf(result, length + 1, template, funcName, asyncKeyword, funcName);
|
568
|
+
JSValueConst ruby_data[2];
|
569
|
+
ruby_data[0] = JS_NewString(data->context, funcName);
|
570
|
+
ruby_data[1] = JS_NewBool(data->context, RTEST(rb_funcall(r_flags, rb_intern("include?"), 1, ID2SYM(rb_intern("async")))));
|
560
571
|
|
561
|
-
JSValue
|
572
|
+
JSValue j_global = JS_GetGlobalObject(data->context);
|
573
|
+
JS_SetPropertyStr(
|
574
|
+
data->context, j_global, funcName,
|
575
|
+
JS_NewCFunctionData(data->context, js_quickjsrb_call_global, 1, 0, 2, ruby_data));
|
576
|
+
JS_FreeValue(data->context, j_global);
|
562
577
|
|
563
|
-
free(result);
|
564
|
-
JS_FreeValue(data->context, j_codeResult);
|
565
578
|
return rb_funcall(r_name, rb_intern("to_sym"), 0, NULL);
|
566
|
-
|
567
|
-
return Qnil;
|
568
579
|
}
|
569
580
|
|
570
581
|
static VALUE vm_m_import(int argc, VALUE *argv, VALUE r_self)
|
data/ext/quickjsrb/quickjsrb.h
CHANGED
@@ -178,6 +178,23 @@ static VALUE r_define_log_class(VALUE r_parent_class)
|
|
178
178
|
return r_log_class;
|
179
179
|
}
|
180
180
|
|
181
|
+
static VALUE r_log_new(const char *severity, VALUE r_row)
|
182
|
+
{
|
183
|
+
VALUE r_log_class = rb_const_get(rb_const_get(rb_const_get(rb_cClass, rb_intern("Quickjs")), rb_intern("VM")), rb_intern("Log"));
|
184
|
+
VALUE r_log = rb_funcall(r_log_class, rb_intern("new"), 0);
|
185
|
+
rb_iv_set(r_log, "@severity", ID2SYM(rb_intern(severity)));
|
186
|
+
rb_iv_set(r_log, "@row", r_row);
|
187
|
+
return r_log;
|
188
|
+
}
|
189
|
+
|
190
|
+
static VALUE r_log_body_new(VALUE r_raw, VALUE r_c)
|
191
|
+
{
|
192
|
+
VALUE r_log_body = rb_hash_new();
|
193
|
+
rb_hash_aset(r_log_body, ID2SYM(rb_intern("raw")), r_raw);
|
194
|
+
rb_hash_aset(r_log_body, ID2SYM(rb_intern("c")), r_c);
|
195
|
+
return r_log_body;
|
196
|
+
}
|
197
|
+
|
181
198
|
// Exceptions
|
182
199
|
|
183
200
|
#define QUICKJSRB_ROOT_RUNTIME_ERROR "RuntimeError"
|
data/lib/quickjs/version.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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hmsk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|