quickjs 0.6.4 → 0.6.5

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: 4a2c456cd81f2a2d5085e720a1872ffc7ee873c49319db8fc0459f2c6336e437
4
- data.tar.gz: a2881627264f8d69bdf04f3773630f17eacd9c03d5fbaa59c521ce7590ff3778
3
+ metadata.gz: 31bcb05285a3c4c80c6e5c06ec043793debf795c75f82362b317d74d3eb8cf96
4
+ data.tar.gz: 9032bf6cb129ea65dc52ae5df5ff394056f440c9de52947ab3029e5c0626c554
5
5
  SHA512:
6
- metadata.gz: 1733ef77baf22132775c06ea988ad66f8e29712a92221513894ab31173fcd165edfa6ca2a32ca855ba5d641abda6613a0419f34baf32a59a47886b98385b4836
7
- data.tar.gz: 910f2e1d364f873261a3a4caa092427101dd64bfeaefb497fafc6caa64dd5ae4fc2d42559f31829a350d7329bef3f887f557afe1449e934516ed10d7e638b25a
6
+ metadata.gz: 2a12da5b5c71c5d350af754f5b50e47104a379aa53cde68625da76bf6b3e165a804b3ec9d764d52f7e8c87a696a870d948d17381bb044b13536b6e96992e9cea
7
+ data.tar.gz: 1b0b05872129c9d3850e66b0e5f77c779bb2554801e132380a0937a2d21d393b23317364e775ab9c0779ba074da157d46fdd6779a1b5f0ba690c55e2cbedccbd
data/README.md CHANGED
@@ -153,7 +153,7 @@ vm.eval_code('console.log("log me", null)')
153
153
  vm.logs #=> Array of Quickjs::VM::Log
154
154
  vm.logs.last.severity #=> :info
155
155
  vm.logs.last.to_s #=> 'log me null'
156
- vm,logs.last.raw #=> ['log me', nil]
156
+ vm.logs.last.raw #=> ['log me', nil]
157
157
  ```
158
158
 
159
159
  ## License
@@ -58,17 +58,9 @@ JSValue to_js_value(JSContext *ctx, VALUE r_value)
58
58
  {
59
59
  VALUE r_json_str = rb_funcall(r_value, rb_intern("to_json"), 0, NULL);
60
60
  char *str = StringValueCStr(r_json_str);
61
- JSValue j_global = JS_GetGlobalObject(ctx);
62
- JSValue j_jsonClass = JS_GetPropertyStr(ctx, j_global, "JSON");
63
- JSValue j_parseFunc = JS_GetPropertyStr(ctx, j_jsonClass, "parse");
64
- JSValue j_str = JS_NewString(ctx, str);
65
- JSValue j_stringified = JS_Call(ctx, j_parseFunc, j_jsonClass, 1, (JSValueConst *)&j_str);
66
- JS_FreeValue(ctx, j_global);
67
- JS_FreeValue(ctx, j_jsonClass);
68
- JS_FreeValue(ctx, j_parseFunc);
69
- JS_FreeValue(ctx, j_str);
61
+ JSValue j_parsed = JS_ParseJSON(ctx, str, strlen(str), "<quickjsrb.c>");
70
62
 
71
- return j_stringified;
63
+ return j_parsed;
72
64
  }
73
65
  default:
74
66
  {
@@ -132,9 +124,7 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
132
124
  {
133
125
  case JS_TAG_INT:
134
126
  {
135
- int int_res = 0;
136
- JS_ToInt32(ctx, &int_res, j_val);
137
- return INT2NUM(int_res);
127
+ return INT2NUM(JS_VALUE_GET_INT(j_val));
138
128
  }
139
129
  case JS_TAG_FLOAT64:
140
130
  {
@@ -142,9 +132,7 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
142
132
  {
143
133
  return QUICKJSRB_SYM(nanId);
144
134
  }
145
- double double_res;
146
- JS_ToFloat64(ctx, &double_res, j_val);
147
- return DBL2NUM(double_res);
135
+ return DBL2NUM(JS_VALUE_GET_FLOAT64(j_val));
148
136
  }
149
137
  case JS_TAG_BOOL:
150
138
  {
@@ -400,7 +388,40 @@ static JSValue js_quickjsrb_log(JSContext *ctx, JSValueConst _this, int argc, JS
400
388
  for (int i = 0; i < argc; i++)
401
389
  {
402
390
  JSValue j_logged = JS_DupValue(ctx, argv[i]);
403
- 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);
391
+ VALUE r_raw;
392
+ if (JS_VALUE_GET_NORM_TAG(j_logged) == JS_TAG_OBJECT && JS_PromiseState(ctx, j_logged) != -1)
393
+ {
394
+ r_raw = rb_str_new2("Promise");
395
+ }
396
+ else if (JS_IsError(ctx, j_logged))
397
+ {
398
+ JSValue j_errorClassName = JS_GetPropertyStr(ctx, j_logged, "name");
399
+ const char *errorClassName = JS_ToCString(ctx, j_errorClassName);
400
+ JS_FreeValue(ctx, j_errorClassName);
401
+
402
+ JSValue j_errorClassMessage = JS_GetPropertyStr(ctx, j_logged, "message");
403
+ const char *errorClassMessage = JS_ToCString(ctx, j_errorClassMessage);
404
+ JS_FreeValue(ctx, j_errorClassMessage);
405
+
406
+ JSValue j_stackTrace = JS_GetPropertyStr(ctx, j_logged, "stack");
407
+ const char *stackTrace = JS_ToCString(ctx, j_stackTrace);
408
+ JS_FreeValue(ctx, j_stackTrace);
409
+
410
+ const char *headlineTemplate = "%s: %s\n%s";
411
+ int length = snprintf(NULL, 0, headlineTemplate, errorClassName, errorClassMessage, stackTrace);
412
+ char *headline = (char *)malloc(length + 1);
413
+ snprintf(headline, length + 1, headlineTemplate, errorClassName, errorClassMessage, stackTrace);
414
+ JS_FreeCString(ctx, errorClassName);
415
+ JS_FreeCString(ctx, errorClassMessage);
416
+ JS_FreeCString(ctx, stackTrace);
417
+
418
+ r_raw = rb_str_new2(headline);
419
+ free(headline);
420
+ }
421
+ else
422
+ {
423
+ r_raw = to_rb_value(ctx, j_logged);
424
+ }
404
425
  const char *body = JS_ToCString(ctx, j_logged);
405
426
  VALUE r_c = rb_str_new2(body);
406
427
  JS_FreeCString(ctx, body);
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quickjs
4
- VERSION = "0.6.4"
4
+ VERSION = "0.6.5"
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.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - hmsk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-28 00:00:00.000000000 Z
11
+ date: 2025-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json