quickjs 0.1.7 → 0.1.8

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: 9a639b7afafb5c155725cba8d7927b7f4e146e21369a65a3902e8fe23ef1e3dd
4
- data.tar.gz: b0e098ae6c193e761f456642cdcaf5396251785d5bc56b5e807fd540a0615f87
3
+ metadata.gz: 558b76a18b29f977c09990bec267141fb9feae1f04ddd4265971c7c24e823b9b
4
+ data.tar.gz: b2dbe1a4a121e92e7f1792690fe755ce021091a4ff402a773b9853c885256dcc
5
5
  SHA512:
6
- metadata.gz: 9ad93ee1c056d68409daeba63515b8afa8555150044e0543a97bb2f3cb64abf5b8f5a5b1fce75e952721b0277a7807ce521289b70b63157cc489032da7da8f7c
7
- data.tar.gz: b36a5f2e7402156e94403c5110ec1acc6141d657b236562b18b57c63c68fd966d4ef25096e3b515f9633093bee2e2a89f65578b4426e0c4184197078c9c2c578
6
+ metadata.gz: 338bc63e7590c7043cf86c7045f3e93c8269110d3daf49be87ef6477cd99086700a6c0a26dcdc8cf7eb2ce43acc025c419812eeb23ab0b155b59ddd335454db5
7
+ data.tar.gz: cb075876cdc10e081b95c40882acbe7ed272dacca56050564888a8df5a68c058c31fa61d9151bddf794d6846c1f00052ae74e3d439c493761bf47154f6fcfdc2
data/README.md CHANGED
@@ -89,6 +89,11 @@ vm = Quickjs::VM.new(
89
89
  vm = Quickjs::VM.new(
90
90
  features: [::Quickjs::MODULE_OS],
91
91
  )
92
+
93
+ # `eval_code` will be interrupted after 1 sec (default: 100 msec)
94
+ vm = Quickjs::VM.new(
95
+ timeout_msec: 1_000,
96
+ )
92
97
  ```
93
98
 
94
99
  #### Dispose VM explicitly
@@ -1,10 +1,15 @@
1
1
  #include "quickjsrb.h"
2
2
 
3
+ typedef struct EvalTime {
4
+ clock_t limit;
5
+ clock_t started_at;
6
+ } EvalTime;
7
+
3
8
  typedef struct VMData {
4
9
  char alive;
5
10
  struct JSContext *context;
6
- struct QuickjsrbRuntimeState *state;
7
11
  struct ProcEntryMap *procs;
12
+ struct EvalTime *eval_time;
8
13
  } VMData;
9
14
 
10
15
  void vm_free(void* data)
@@ -123,7 +128,7 @@ VALUE to_rb_value(JSValue jsv, JSContext *ctx) {
123
128
  case JS_TAG_OBJECT: {
124
129
  int promiseState = JS_PromiseState(ctx, jsv);
125
130
  if (promiseState == JS_PROMISE_FULFILLED || promiseState == JS_PROMISE_PENDING) {
126
- return to_rb_value(js_std_await(ctx, jsv), ctx);
131
+ return to_rb_value(js_std_await(ctx, jsv), ctx); // should have timeout
127
132
  } else if (promiseState == JS_PROMISE_REJECTED) {
128
133
  return to_rb_value(JS_Throw(ctx, JS_PromiseResult(ctx, jsv)), ctx);
129
134
  }
@@ -215,6 +220,8 @@ VALUE vm_m_initialize(int argc, VALUE* argv, VALUE self)
215
220
  if (NIL_P(r_maxStackSize)) r_maxStackSize = UINT2NUM(1024 * 1024 * 4);
216
221
  VALUE r_features = rb_hash_aref(r_opts, ID2SYM(rb_intern("features")));
217
222
  if (NIL_P(r_features)) r_features = rb_ary_new();
223
+ VALUE r_timeout_msec = rb_hash_aref(r_opts, ID2SYM(rb_intern("timeout_msec")));
224
+ if (NIL_P(r_timeout_msec)) r_timeout_msec= UINT2NUM(100);
218
225
 
219
226
  VMData *data;
220
227
  TypedData_Get_Struct(self, VMData, &vm_type, data);
@@ -222,6 +229,10 @@ VALUE vm_m_initialize(int argc, VALUE* argv, VALUE self)
222
229
  JSRuntime *runtime = JS_NewRuntime();
223
230
  data->context = JS_NewContext(runtime);
224
231
  data->procs = create_proc_entries();
232
+
233
+ EvalTime *eval_time = malloc(sizeof(EvalTime));
234
+ data->eval_time = eval_time;
235
+ data->eval_time->limit = (clock_t)(CLOCKS_PER_SEC * NUM2UINT(r_timeout_msec) / 1000);
225
236
  data->alive = 1;
226
237
  JS_SetContextOpaque(data->context, data);
227
238
 
@@ -265,6 +276,11 @@ VALUE vm_m_initialize(int argc, VALUE* argv, VALUE self)
265
276
  return self;
266
277
  }
267
278
 
279
+ static int interrupt_handler(JSRuntime *runtime, void *opaque) {
280
+ EvalTime *eval_time = opaque;
281
+ return clock() >= eval_time->started_at + eval_time->limit ? 1 : 0;
282
+ }
283
+
268
284
  VALUE vm_m_evalCode(VALUE self, VALUE r_code)
269
285
  {
270
286
  VMData *data;
@@ -274,6 +290,10 @@ VALUE vm_m_evalCode(VALUE self, VALUE r_code)
274
290
  rb_raise(rb_eRuntimeError, "Quickjs::VM was disposed");
275
291
  return Qnil;
276
292
  }
293
+
294
+ data->eval_time->started_at = clock();
295
+ JS_SetInterruptHandler(JS_GetRuntime(data->context), interrupt_handler, data->eval_time);
296
+
277
297
  char *code = StringValueCStr(r_code);
278
298
  JSValue codeResult = JS_Eval(data->context, code, strlen(code), "<code>", JS_EVAL_TYPE_GLOBAL);
279
299
  VALUE result = to_rb_value(codeResult, data->context);
@@ -317,6 +337,7 @@ VALUE vm_m_dispose(VALUE self)
317
337
  TypedData_Get_Struct(self, VMData, &vm_type, data);
318
338
 
319
339
  JSRuntime *runtime = JS_GetRuntime(data->context);
340
+ JS_SetInterruptHandler(runtime, NULL, NULL);
320
341
  js_std_free_handlers(runtime);
321
342
  free_proc_entry_map(data->procs);
322
343
  JS_FreeContext(data->context);
@@ -10,6 +10,7 @@
10
10
  #include <stdint.h>
11
11
  #include <stdio.h>
12
12
  #include <string.h>
13
+ #include <time.h>
13
14
 
14
15
  #include "procs.h"
15
16
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quickjs
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
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.7
4
+ version: 0.1.8
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-09 00:00:00.000000000 Z
11
+ date: 2024-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json