quickjs 0.3.0 → 0.4.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 +67 -24
- 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: 49e231625e712ca98ffcab031b747d11392608eb840b2cafa6881173a5be9ac8
|
4
|
+
data.tar.gz: '099960b9c78f6f7a75990386186a0b149102d73ccbbbb7a1db7e52e120c57833'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cad539bfe3b3a7e254ef4c1a2919c7a12afd29e67e5729acc2f8356896898eef71f1f46c35a62e351b1ac03ba0b8e9fe357c4f69781d5b8a203525068e22261
|
7
|
+
data.tar.gz: d4fcdfe872d91b0fd86bcccd9e58262b233ee99d31a529ba288583e2953ea191f4b9269dacbac9a47256f5d2111f737741fce4d5d26cba5e4fcc97bc450be96b
|
data/ext/quickjsrb/quickjsrb.c
CHANGED
@@ -87,6 +87,23 @@ JSValue to_js_value(JSContext *ctx, VALUE r_value)
|
|
87
87
|
}
|
88
88
|
}
|
89
89
|
|
90
|
+
VALUE find_ruby_error(JSContext *ctx, JSValue j_error)
|
91
|
+
{
|
92
|
+
JSValue j_errorOriginalRubyObjectId = JS_GetPropertyStr(ctx, j_error, "rb_object_id");
|
93
|
+
int errorOriginalRubyObjectId = 0;
|
94
|
+
if (JS_VALUE_GET_NORM_TAG(j_errorOriginalRubyObjectId) == JS_TAG_INT)
|
95
|
+
{
|
96
|
+
JS_ToInt32(ctx, &errorOriginalRubyObjectId, j_errorOriginalRubyObjectId);
|
97
|
+
JS_FreeValue(ctx, j_errorOriginalRubyObjectId);
|
98
|
+
if (errorOriginalRubyObjectId > 0)
|
99
|
+
{
|
100
|
+
// may be nice if cover the case of object is missing
|
101
|
+
return rb_funcall(rb_const_get(rb_cClass, rb_intern("ObjectSpace")), rb_intern("_id2ref"), 1, INT2NUM(errorOriginalRubyObjectId));
|
102
|
+
}
|
103
|
+
}
|
104
|
+
return Qnil;
|
105
|
+
}
|
106
|
+
|
90
107
|
VALUE r_try_json_parse(VALUE r_str)
|
91
108
|
{
|
92
109
|
return rb_funcall(rb_const_get(rb_cClass, rb_intern("JSON")), rb_intern("parse"), 1, r_str);
|
@@ -135,19 +152,12 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
135
152
|
|
136
153
|
if (JS_IsError(ctx, j_val))
|
137
154
|
{
|
138
|
-
|
139
|
-
|
140
|
-
if (JS_VALUE_GET_NORM_TAG(j_errorOriginalRubyObjectId) == JS_TAG_INT)
|
155
|
+
VALUE r_maybe_ruby_error = find_ruby_error(ctx, j_val);
|
156
|
+
if (!NIL_P(r_maybe_ruby_error))
|
141
157
|
{
|
142
|
-
|
143
|
-
JS_FreeValue(ctx, j_errorOriginalRubyObjectId);
|
144
|
-
if (errorOriginalRubyObjectId > 0)
|
145
|
-
{
|
146
|
-
// may be nice if cover the case of object is missing
|
147
|
-
return rb_funcall(rb_const_get(rb_cClass, rb_intern("ObjectSpace")), rb_intern("_id2ref"), 1, INT2NUM(errorOriginalRubyObjectId));
|
148
|
-
}
|
158
|
+
return r_maybe_ruby_error;
|
149
159
|
}
|
150
|
-
// will support other errors
|
160
|
+
// will support other errors like just returning an instance of Error
|
151
161
|
}
|
152
162
|
|
153
163
|
JSValue j_global = JS_GetGlobalObject(ctx);
|
@@ -189,19 +199,12 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
189
199
|
JSValue j_exceptionVal = JS_GetException(ctx);
|
190
200
|
if (JS_IsError(ctx, j_exceptionVal))
|
191
201
|
{
|
192
|
-
|
193
|
-
|
194
|
-
if (JS_VALUE_GET_NORM_TAG(j_errorOriginalRubyObjectId) == JS_TAG_INT)
|
202
|
+
VALUE r_maybe_ruby_error = find_ruby_error(ctx, j_exceptionVal);
|
203
|
+
if (!NIL_P(r_maybe_ruby_error))
|
195
204
|
{
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
{
|
200
|
-
JS_FreeValue(ctx, j_exceptionVal);
|
201
|
-
// may be nice if cover the case of object is missing
|
202
|
-
rb_exc_raise(rb_funcall(rb_const_get(rb_cClass, rb_intern("ObjectSpace")), rb_intern("_id2ref"), 1, INT2NUM(errorOriginalRubyObjectId)));
|
203
|
-
return Qnil;
|
204
|
-
}
|
205
|
+
JS_FreeValue(ctx, j_exceptionVal);
|
206
|
+
rb_exc_raise(r_maybe_ruby_error);
|
207
|
+
return Qnil;
|
205
208
|
}
|
206
209
|
|
207
210
|
JSValue j_errorClassName = JS_GetPropertyStr(ctx, j_exceptionVal, "name");
|
@@ -210,8 +213,30 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
210
213
|
JSValue j_errorClassMessage = JS_GetPropertyStr(ctx, j_exceptionVal, "message");
|
211
214
|
const char *errorClassMessage = JS_ToCString(ctx, j_errorClassMessage);
|
212
215
|
|
216
|
+
JSValue j_stackTrace = JS_GetPropertyStr(ctx, j_exceptionVal, "stack");
|
217
|
+
const char *stackTrace = JS_ToCString(ctx, j_stackTrace);
|
218
|
+
const char *headlineTemplate = "Uncaught %s: %s\n%s";
|
219
|
+
int length = snprintf(NULL, 0, headlineTemplate, errorClassName, errorClassMessage, stackTrace);
|
220
|
+
char *headline = (char *)malloc(length + 1);
|
221
|
+
snprintf(headline, length + 1, headlineTemplate, errorClassName, errorClassMessage, stackTrace);
|
222
|
+
|
223
|
+
VMData *data = JS_GetContextOpaque(ctx);
|
224
|
+
VALUE r_log_class = rb_const_get(rb_const_get(rb_const_get(rb_cClass, rb_intern("Quickjs")), rb_intern("VM")), rb_intern("Log"));
|
225
|
+
VALUE r_log = rb_funcall(r_log_class, rb_intern("new"), 0);
|
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);
|
234
|
+
|
213
235
|
JS_FreeValue(ctx, j_errorClassMessage);
|
214
236
|
JS_FreeValue(ctx, j_errorClassName);
|
237
|
+
JS_FreeValue(ctx, j_stackTrace);
|
238
|
+
JS_FreeCString(ctx, stackTrace);
|
239
|
+
free(headline);
|
215
240
|
|
216
241
|
VALUE r_error_class, r_error_message = rb_str_new2(errorClassMessage);
|
217
242
|
if (is_native_error_name(errorClassName))
|
@@ -240,8 +265,26 @@ VALUE to_rb_value(JSContext *ctx, JSValue j_val)
|
|
240
265
|
else // exception without Error object
|
241
266
|
{
|
242
267
|
const char *errorMessage = JS_ToCString(ctx, j_exceptionVal);
|
243
|
-
|
268
|
+
const char *headlineTemplate = "Uncaught '%s'";
|
269
|
+
int length = snprintf(NULL, 0, headlineTemplate, errorMessage);
|
270
|
+
char *headline = (char *)malloc(length + 1);
|
271
|
+
snprintf(headline, length + 1, headlineTemplate, errorMessage);
|
272
|
+
|
273
|
+
VMData *data = JS_GetContextOpaque(ctx);
|
274
|
+
VALUE r_log_class = rb_const_get(rb_const_get(rb_const_get(rb_cClass, rb_intern("Quickjs")), rb_intern("VM")), rb_intern("Log"));
|
275
|
+
VALUE r_log = rb_funcall(r_log_class, rb_intern("new"), 0);
|
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);
|
284
|
+
|
285
|
+
free(headline);
|
244
286
|
|
287
|
+
VALUE r_error_message = rb_sprintf("%s", errorMessage);
|
245
288
|
JS_FreeCString(ctx, errorMessage);
|
246
289
|
JS_FreeValue(ctx, j_exceptionVal);
|
247
290
|
rb_exc_raise(rb_funcall(QUICKJSRB_ERROR_FOR(QUICKJSRB_ROOT_RUNTIME_ERROR), rb_intern("new"), 2, r_error_message, Qnil));
|
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.4.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-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|