quickjs 0.1.6 → 0.1.7

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: 6fa772eb6185fa2bc744d7bb7b0cdb2024ccaf2d2ce34cb221139aaf2e819371
4
- data.tar.gz: de4b5651b61eaaafe975ba56ae24b954c28efc9ea0ba2a6fb34f149b62d4aa58
3
+ metadata.gz: 9a639b7afafb5c155725cba8d7927b7f4e146e21369a65a3902e8fe23ef1e3dd
4
+ data.tar.gz: b0e098ae6c193e761f456642cdcaf5396251785d5bc56b5e807fd540a0615f87
5
5
  SHA512:
6
- metadata.gz: 1095f8d04c0c90756506b8bd16a519b207b3b2a0eda64abecec8fb541a0a5a5c0e11240abce18b03e4ee4d7b4c3a53138513218f12afa4d7e46462f3a93680ef
7
- data.tar.gz: 3237ad5b23217199900665605629c5b69c0333ea5951f3a43cf2e26ac65d6a718ff94f262fdaeb5bf0c3b132e60cb0561d60a121045a26bb201bbfd63e8e3a9c
6
+ metadata.gz: 9ad93ee1c056d68409daeba63515b8afa8555150044e0543a97bb2f3cb64abf5b8f5a5b1fce75e952721b0277a7807ce521289b70b63157cc489032da7da8f7c
7
+ data.tar.gz: b36a5f2e7402156e94403c5110ec1acc6141d657b236562b18b57c63c68fd966d4ef25096e3b515f9633093bee2e2a89f65578b4426e0c4184197078c9c2c578
@@ -11,7 +11,7 @@ $srcs = [
11
11
  'cutils.c',
12
12
  'quickjs.c',
13
13
  'quickjs-libc.c',
14
- 'quickjsrb-runtime-state.c',
14
+ 'procs.c',
15
15
  'quickjsrb.c',
16
16
  ]
17
17
 
@@ -1,4 +1,4 @@
1
- #include "quickjsrb-runtime-state.h"
1
+ #include "procs.h"
2
2
 
3
3
  unsigned int hash(const char *key) {
4
4
  unsigned long int value = 0;
@@ -8,7 +8,7 @@ unsigned int hash(const char *key) {
8
8
  for (; i < key_len; ++i) {
9
9
  value = value * 37 + key[i];
10
10
  }
11
- return value % MAX_NUM_OF_PROC;
11
+ return value % MAX_NUM_OF_PROCS;
12
12
  }
13
13
 
14
14
  ProcEntry *create_proc_entry(const char *key, VALUE proc) {
@@ -21,8 +21,8 @@ ProcEntry *create_proc_entry(const char *key, VALUE proc) {
21
21
 
22
22
  ProcEntryMap *create_proc_entries() {
23
23
  ProcEntryMap *entryMap = malloc(sizeof(ProcEntryMap));
24
- entryMap->entries = malloc(sizeof(ProcEntry *) * MAX_NUM_OF_PROC);
25
- for (int i = 0; i < MAX_NUM_OF_PROC; ++i) {
24
+ entryMap->entries = malloc(sizeof(ProcEntry *) * MAX_NUM_OF_PROCS);
25
+ for (int i = 0; i < MAX_NUM_OF_PROCS; ++i) {
26
26
  entryMap->entries[i] = NULL;
27
27
  }
28
28
  return entryMap;
@@ -66,7 +66,7 @@ VALUE get_proc(ProcEntryMap *entryMap, const char *key) {
66
66
  }
67
67
 
68
68
  void free_proc_entry_map(ProcEntryMap *entryMap) {
69
- for (int i = 0; i < MAX_NUM_OF_PROC; ++i) {
69
+ for (int i = 0; i < MAX_NUM_OF_PROCS; ++i) {
70
70
  ProcEntry *entry = entryMap->entries[i];
71
71
  while (entry != NULL) {
72
72
  ProcEntry *temp = entry;
@@ -78,15 +78,3 @@ void free_proc_entry_map(ProcEntryMap *entryMap) {
78
78
  free(entryMap->entries);
79
79
  free(entryMap);
80
80
  }
81
-
82
- QuickjsrbRuntimeState *create_quickjsrb_runtime_state() {
83
- QuickjsrbRuntimeState *state = malloc(sizeof(QuickjsrbRuntimeState));
84
- state->procs = create_proc_entries();
85
-
86
- return state;
87
- }
88
-
89
- void free_quickjsrb_runtime_state(QuickjsrbRuntimeState *state) {
90
- free_proc_entry_map(state->procs);
91
- free(state);
92
- }
@@ -1,6 +1,6 @@
1
1
  #include "ruby.h"
2
2
 
3
- #define MAX_NUM_OF_PROC 100
3
+ #define MAX_NUM_OF_PROCS 100
4
4
 
5
5
  typedef struct ProcEntry {
6
6
  char *key;
@@ -12,11 +12,7 @@ typedef struct ProcEntryMap {
12
12
  ProcEntry **entries;
13
13
  } ProcEntryMap;
14
14
 
15
- typedef struct QuickjsrbRuntimeState {
16
- ProcEntryMap *procs;
17
- } QuickjsrbRuntimeState;
18
-
19
- QuickjsrbRuntimeState *create_quickjsrb_runtime_state();
20
- VALUE get_proc(ProcEntryMap *entryMap, const char *key);
15
+ ProcEntryMap *create_proc_entries();
21
16
  void set_proc(ProcEntryMap *entryMap, const char *key, VALUE proc);
22
- void free_quickjsrb_runtime_state(QuickjsrbRuntimeState *state);
17
+ VALUE get_proc(ProcEntryMap *entryMap, const char *key);
18
+ void free_proc_entry_map(ProcEntryMap *entryMap);
@@ -1,64 +1,37 @@
1
1
  #include "quickjsrb.h"
2
- #include "quickjsrb-runtime-state.h"
3
2
 
4
- struct qvmdata {
3
+ typedef struct VMData {
5
4
  char alive;
6
5
  struct JSContext *context;
7
6
  struct QuickjsrbRuntimeState *state;
8
- };
7
+ struct ProcEntryMap *procs;
8
+ } VMData;
9
9
 
10
- void qvm_free(void* data)
10
+ void vm_free(void* data)
11
11
  {
12
12
  free(data);
13
13
  }
14
14
 
15
- size_t qvm_size(const void* data)
15
+ size_t vm_size(const void* data)
16
16
  {
17
17
  return sizeof(data);
18
18
  }
19
19
 
20
- static const rb_data_type_t qvm_type = {
21
- .wrap_struct_name = "qvm",
20
+ static const rb_data_type_t vm_type = {
21
+ .wrap_struct_name = "vm",
22
22
  .function = {
23
23
  .dmark = NULL,
24
- .dfree = qvm_free,
25
- .dsize = qvm_size,
24
+ .dfree = vm_free,
25
+ .dsize = vm_size,
26
26
  },
27
27
  .data = NULL,
28
28
  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
29
29
  };
30
30
 
31
- VALUE qvm_alloc(VALUE self)
31
+ VALUE vm_alloc(VALUE self)
32
32
  {
33
- struct qvmdata *data;
34
-
35
- return TypedData_Make_Struct(self, struct qvmdata, &qvm_type, data);
36
- }
37
-
38
- static JSValue js_quickjsrb_call_global (JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
39
- struct qvmdata *data = JS_GetContextOpaque(ctx);
40
- JSValue maybeFuncName = JS_ToString(ctx, argv[0]);
41
- const char *funcName = JS_ToCString(ctx, maybeFuncName);
42
-
43
- VALUE proc = get_proc(data->state->procs, funcName);
44
- if (proc == Qnil) {
45
- return JS_NewString(ctx, "missing");
46
- }
47
- JS_FreeValue(ctx, maybeFuncName);
48
-
49
- // TODO: support multiple args
50
- JSValue maybeString;
51
- VALUE r_array = rb_ary_new2(argc);
52
- for (int i = 1; i < argc; ++i) {
53
- maybeString = JS_ToString(ctx, argv[i]);
54
- const char *msg = JS_ToCString(ctx, maybeString);
55
- rb_ary_push(r_array, rb_str_new2(msg));
56
- }
57
- JS_FreeValue(ctx, maybeString);
58
-
59
- VALUE r_result = rb_funcall(proc, rb_intern("call"), argc - 1, r_array);
60
- char *result = StringValueCStr(r_result);
61
- return JS_NewString(ctx, result);
33
+ VMData *data;
34
+ return TypedData_Make_Struct(self, VMData, &vm_type, data);
62
35
  }
63
36
 
64
37
  VALUE rb_mQuickjs;
@@ -68,7 +41,63 @@ const char *nanId = "NaN";
68
41
  const char *featureStdId = "feature_std";
69
42
  const char *featureOsId = "feature_os";
70
43
 
71
- VALUE to_rb_value (JSValue jsv, JSContext *ctx) {
44
+ JSValue to_js_value(JSContext *ctx, VALUE r_value) {
45
+ switch (TYPE(r_value)) {
46
+ case T_NIL:
47
+ return JS_NULL;
48
+ case T_FIXNUM:
49
+ case T_FLOAT: {
50
+ VALUE r_str = rb_funcall(r_value, rb_intern("to_s"), 0, NULL);
51
+ char *str = StringValueCStr(r_str);
52
+ JSValue global = JS_GetGlobalObject(ctx);
53
+ JSValue numberClass = JS_GetPropertyStr(ctx, global, "Number");
54
+ JSValue j_str = JS_NewString(ctx, str);
55
+ JSValue stringified = JS_Call(ctx, numberClass, JS_UNDEFINED, 1, &j_str);
56
+ JS_FreeValue(ctx, global);
57
+ JS_FreeValue(ctx, numberClass);
58
+
59
+ return stringified;
60
+ }
61
+ case T_STRING: {
62
+ char *str = StringValueCStr(r_value);
63
+
64
+ return JS_NewString(ctx, str);
65
+ }
66
+ case T_SYMBOL: {
67
+ VALUE r_str = rb_funcall(r_value, rb_intern("to_s"), 0, NULL);
68
+ char *str = StringValueCStr(r_str);
69
+
70
+ return JS_NewString(ctx, str);
71
+ }
72
+ case T_TRUE:
73
+ return JS_TRUE;
74
+ case T_FALSE:
75
+ return JS_FALSE;
76
+ case T_HASH:
77
+ case T_ARRAY: {
78
+ VALUE r_json_str = rb_funcall(r_value, rb_intern("to_json"), 0, NULL);
79
+ char *str = StringValueCStr(r_json_str);
80
+ JSValue global = JS_GetGlobalObject(ctx);
81
+ JSValue jsonClass = JS_GetPropertyStr(ctx, global, "JSON");
82
+ JSValue parseFunc = JS_GetPropertyStr(ctx, jsonClass, "parse");
83
+ JSValue j_str = JS_NewString(ctx, str);
84
+ JSValue stringified = JS_Call(ctx, parseFunc, jsonClass, 1, &j_str);
85
+ JS_FreeValue(ctx, global);
86
+ JS_FreeValue(ctx, parseFunc);
87
+ JS_FreeValue(ctx, jsonClass);
88
+
89
+ return stringified;
90
+ }
91
+ default: {
92
+ VALUE r_inspect_str = rb_funcall(r_value, rb_intern("inspect"), 0, NULL);
93
+ char *str = StringValueCStr(r_inspect_str);
94
+
95
+ return JS_NewString(ctx, str);
96
+ }
97
+ }
98
+ }
99
+
100
+ VALUE to_rb_value(JSValue jsv, JSContext *ctx) {
72
101
  switch(JS_VALUE_GET_NORM_TAG(jsv)) {
73
102
  case JS_TAG_INT: {
74
103
  int int_res = 0;
@@ -159,7 +188,22 @@ VALUE to_rb_value (JSValue jsv, JSContext *ctx) {
159
188
  }
160
189
  }
161
190
 
162
- VALUE qvm_m_initialize(int argc, VALUE* argv, VALUE self)
191
+ static JSValue js_quickjsrb_call_global(JSContext *ctx, JSValueConst _this, int _argc, JSValueConst *argv) {
192
+ VMData *data = JS_GetContextOpaque(ctx);
193
+ JSValue maybeFuncName = JS_ToString(ctx, argv[0]);
194
+ const char *funcName = JS_ToCString(ctx, maybeFuncName);
195
+ JS_FreeValue(ctx, maybeFuncName);
196
+
197
+ VALUE proc = get_proc(data->procs, funcName);
198
+ if (proc == Qnil) { // Shouldn't happen
199
+ return JS_ThrowReferenceError(ctx, "Proc `%s` is not defined", funcName);
200
+ }
201
+
202
+ VALUE r_result = rb_apply(proc, rb_intern("call"), to_rb_value(argv[1], ctx));
203
+ return to_js_value(ctx, r_result);
204
+ }
205
+
206
+ VALUE vm_m_initialize(int argc, VALUE* argv, VALUE self)
163
207
  {
164
208
  VALUE r_opts;
165
209
  rb_scan_args(argc, argv, ":", &r_opts);
@@ -172,13 +216,12 @@ VALUE qvm_m_initialize(int argc, VALUE* argv, VALUE self)
172
216
  VALUE r_features = rb_hash_aref(r_opts, ID2SYM(rb_intern("features")));
173
217
  if (NIL_P(r_features)) r_features = rb_ary_new();
174
218
 
175
- struct qvmdata *data;
176
- TypedData_Get_Struct(self, struct qvmdata, &qvm_type, data);
219
+ VMData *data;
220
+ TypedData_Get_Struct(self, VMData, &vm_type, data);
177
221
 
178
- QuickjsrbRuntimeState *state= create_quickjsrb_runtime_state();
179
222
  JSRuntime *runtime = JS_NewRuntime();
180
223
  data->context = JS_NewContext(runtime);
181
- data->state = state;
224
+ data->procs = create_proc_entries();
182
225
  data->alive = 1;
183
226
  JS_SetContextOpaque(data->context, data);
184
227
 
@@ -222,10 +265,10 @@ VALUE qvm_m_initialize(int argc, VALUE* argv, VALUE self)
222
265
  return self;
223
266
  }
224
267
 
225
- VALUE qvm_m_evalCode(VALUE self, VALUE r_code)
268
+ VALUE vm_m_evalCode(VALUE self, VALUE r_code)
226
269
  {
227
- struct qvmdata *data;
228
- TypedData_Get_Struct(self, struct qvmdata, &qvm_type, data);
270
+ VMData *data;
271
+ TypedData_Get_Struct(self, VMData, &vm_type, data);
229
272
 
230
273
  if (data->alive < 1) {
231
274
  rb_raise(rb_eRuntimeError, "Quickjs::VM was disposed");
@@ -239,19 +282,19 @@ VALUE qvm_m_evalCode(VALUE self, VALUE r_code)
239
282
  return result;
240
283
  }
241
284
 
242
- VALUE qvm_m_defineGlobalFunction(VALUE self, VALUE r_name)
285
+ VALUE vm_m_defineGlobalFunction(VALUE self, VALUE r_name)
243
286
  {
244
287
  rb_need_block();
245
288
 
246
- struct qvmdata *data;
247
- TypedData_Get_Struct(self, struct qvmdata, &qvm_type, data);
289
+ VMData *data;
290
+ TypedData_Get_Struct(self, VMData, &vm_type, data);
248
291
 
249
292
  if (rb_block_given_p()) {
250
293
  VALUE proc = rb_block_proc();
251
294
 
252
295
  char *funcName = StringValueCStr(r_name);
253
296
 
254
- set_proc(data->state->procs, funcName, proc);
297
+ set_proc(data->procs, funcName, proc);
255
298
 
256
299
  const char* template = "globalThis.__ruby['%s'] = (...args) => rubyGlobal('%s', args);\nglobalThis['%s'] = globalThis.__ruby['%s'];\n";
257
300
  int length = snprintf(NULL, 0, template, funcName, funcName, funcName, funcName);
@@ -268,14 +311,14 @@ VALUE qvm_m_defineGlobalFunction(VALUE self, VALUE r_name)
268
311
  return Qnil;
269
312
  }
270
313
 
271
- VALUE qvm_m_dispose(VALUE self)
314
+ VALUE vm_m_dispose(VALUE self)
272
315
  {
273
- struct qvmdata *data;
274
- TypedData_Get_Struct(self, struct qvmdata, &qvm_type, data);
316
+ VMData *data;
317
+ TypedData_Get_Struct(self, VMData, &vm_type, data);
275
318
 
276
319
  JSRuntime *runtime = JS_GetRuntime(data->context);
277
320
  js_std_free_handlers(runtime);
278
- free_quickjsrb_runtime_state(data->state);
321
+ free_proc_entry_map(data->procs);
279
322
  JS_FreeContext(data->context);
280
323
  JS_FreeRuntime(runtime);
281
324
  data->alive = 0;
@@ -295,9 +338,9 @@ Init_quickjsrb(void)
295
338
  rb_define_const(valueClass, "NAN", ID2SYM(rb_intern(nanId)));
296
339
 
297
340
  VALUE vmClass = rb_define_class_under(rb_mQuickjs, "VM", rb_cObject);
298
- rb_define_alloc_func(vmClass, qvm_alloc);
299
- rb_define_method(vmClass, "initialize", qvm_m_initialize, -1);
300
- rb_define_method(vmClass, "eval_code", qvm_m_evalCode, 1);
301
- rb_define_method(vmClass, "define_function", qvm_m_defineGlobalFunction, 1);
302
- rb_define_method(vmClass, "dispose!", qvm_m_dispose, 0);
341
+ rb_define_alloc_func(vmClass, vm_alloc);
342
+ rb_define_method(vmClass, "initialize", vm_m_initialize, -1);
343
+ rb_define_method(vmClass, "eval_code", vm_m_evalCode, 1);
344
+ rb_define_method(vmClass, "define_function", vm_m_defineGlobalFunction, 1);
345
+ rb_define_method(vmClass, "dispose!", vm_m_dispose, 0);
303
346
  }
@@ -11,4 +11,6 @@
11
11
  #include <stdio.h>
12
12
  #include <string.h>
13
13
 
14
+ #include "procs.h"
15
+
14
16
  #endif /* QUICKJSRB_H */
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quickjs
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
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.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - hmsk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-08 00:00:00.000000000 Z
11
+ date: 2024-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -37,6 +37,8 @@ files:
37
37
  - README.md
38
38
  - Rakefile
39
39
  - ext/quickjsrb/extconf.rb
40
+ - ext/quickjsrb/procs.c
41
+ - ext/quickjsrb/procs.h
40
42
  - ext/quickjsrb/quickjs/LICENSE
41
43
  - ext/quickjsrb/quickjs/cutils.c
42
44
  - ext/quickjsrb/quickjs/cutils.h
@@ -62,8 +64,6 @@ files:
62
64
  - ext/quickjsrb/quickjs/run-test262.c
63
65
  - ext/quickjsrb/quickjs/unicode_gen.c
64
66
  - ext/quickjsrb/quickjs/unicode_gen_def.h
65
- - ext/quickjsrb/quickjsrb-runtime-state.c
66
- - ext/quickjsrb/quickjsrb-runtime-state.h
67
67
  - ext/quickjsrb/quickjsrb.c
68
68
  - ext/quickjsrb/quickjsrb.h
69
69
  - lib/quickjs.rb