quickjs 0.1.2 → 0.1.4

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: 5ac1abd90f15750c0ed0da7c82804862906def1d584baa5fefb4889682513d35
4
- data.tar.gz: 59ee4e54ccc5743286f38a4b4e325486108399e432f92f12b430de9c68accd28
3
+ metadata.gz: 3e230674190897ed1edcd6b9918deb3f9f073746f28410bdbe3e20e7622baea0
4
+ data.tar.gz: 8aeddfc2188ab5c1c34216a9f0e853339d784d2476967a2309171a654e633cc0
5
5
  SHA512:
6
- metadata.gz: 3c5ba0d56904c52f426440429c3243285572591c4803bcd21649cacef8e40168f9b289867531b8f5ad2f15a533cca31714a931ac6f04062dc7a22a53e22fae95
7
- data.tar.gz: 8fb7571445aedcac86a0cf897fbbf2e6390b40190838a7bbdbd91742ababc893399704afbeeacd599c8ddbd74429c61038889e47e50dc55fc4b35e65630c889a
6
+ metadata.gz: dd1069fadcd70b58c8fe25d88f091304be1956cc43d62f64a389671ef0ed7ee79b53023852bdfe4be321f2aee53d3c00784ef7d3d2d1a329bad370087f388e74
7
+ data.tar.gz: cc4b076a5cfa578805c4108377ab148d4a5c4ec78b65fb6247dd0e906fbf083e0762173926bf4f6cbd37e469ad58f474a276df38a11e834d2207c6ad688b73cb
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ## [0.1.1] - 2024-06-14
1
+ ## [0.1.4] - 2024-06-14
2
2
 
3
3
  - Initial release
4
4
  - See `test/quickjs_test.rb` for supported features
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # quickjs.rb
2
+
3
+ A Ruby wrapper for [QuickJS](https://bellard.org/quickjs) to run JavaScript codes via Ruby with a smaller footprint.
4
+
5
+ [![Gem Version](https://img.shields.io/gem/v/quickjs?style=for-the-badge)](https://rubygems.org/gems/quickjs) [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/hmsk/quickjs.rb/main?style=for-the-badge)](https://github.com/hmsk/quickjs.rb/actions/workflows/main.yml)
6
+
7
+
8
+ ## Installation
9
+
10
+ ```
11
+ gem install quickjs
12
+ ```
13
+
14
+ ```rb
15
+ gem 'quickjs'
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### `Quickjs.evalCode`: Evaluate JavaScript code
21
+
22
+ ```rb
23
+ require 'quickjs'
24
+
25
+ Quickjs.evalCode('const fn = (n, pow) => n ** pow; fn(2,8);') # => 256
26
+ Quickjs.evalCode('const fn = (name) => `Hi, ${name}!`; fn("Itadori");') # => "Hi, Itadori!
27
+ Quickjs.evalCode("const isOne = (n) => 1 === n; func(1);") #=> true (TrueClass)
28
+ Quickjs.evalCode("const isOne = (n) => 1 === n; func(3);") #=> false (FalseClass)
29
+
30
+ # When code returns 'object' for `typeof`, the result is converted via JSON.stringify (JS) -> JSON.parse (Ruby)
31
+ Quickjs.evalCode("[1,2,3]") #=> [1, 2, 3] (Array)
32
+ Quickjs.evalCode("({ a: '1', b: 1 })") #=> { 'a' => '1', 'b' => 1 } (Hash)
33
+
34
+ Quickjs.evalCode("null") #=> nil
35
+ Quickjs.evalCode('const obj = {}; obj.missingKey;') # => :undefined (Quickjs::Value::Undefined)
36
+ Quickjs.evalCode("Number('whatever')") #=> :NaN (Quickjs::Value::NAN)
37
+ ```
38
+
39
+ #### Limit resources
40
+
41
+ ```rb
42
+ # 1GB memory limit
43
+ Quickjs.evalCode(code, { memoryLimit: 1024 ** 3 })
44
+
45
+ # 1MB max stack size
46
+ Quickjs.evalCode(code, { memoryLimit: 1024 ** 2 })
47
+ ```
48
+
49
+ #### Enable built-in modules
50
+
51
+ ```rb
52
+ # enable std module
53
+ # https://bellard.org/quickjs/quickjs.html#std-module
54
+ Quickjs.evalCode(code, { features: [Quickjs::MODULE_STD] })
55
+
56
+ # enable os module
57
+ # https://bellard.org/quickjs/quickjs.html#os-module
58
+ Quickjs.evalCode(code, { features: [Quickjs::MODULE_OS] })
59
+ ```
60
+
61
+ ## License
62
+
63
+ Every file in `ext/quickjsrb/quickjs` is licensed under [the MIT License Copyright 2017-2021 by Fabrice Bellard and Charlie Goron](/ext/quickjsrb/quickjs/LICENSE).
64
+ For otherwise, [the MIT License, Copyright 2024 by Kengo Hamasaki](/LICENSE).
@@ -49,7 +49,6 @@ VALUE rb_module_eval_js_code(
49
49
  JSValue res = JS_Eval(ctx, code, strlen(code), "<code>", JS_EVAL_TYPE_GLOBAL);
50
50
 
51
51
  VALUE result;
52
- int r = 0;
53
52
  if (JS_IsException(res)) {
54
53
  rb_raise(rb_eRuntimeError, "Something happened by evaluating as JavaScript code");
55
54
  result = Qnil;
@@ -60,7 +59,9 @@ VALUE rb_module_eval_js_code(
60
59
  JSValue strigified = JS_Call(ctx, stringifyFunc, jsonClass, 1, &res);
61
60
 
62
61
  const char *msg = JS_ToCString(ctx, strigified);
63
- result = rb_str_new2(msg);
62
+ VALUE rbString = rb_str_new2(msg);
63
+ VALUE rb_cJson = rb_const_get(rb_cClass, rb_intern("JSON"));
64
+ result = rb_funcall(rb_cJson, rb_intern("parse"), 1, rbString);
64
65
 
65
66
  JS_FreeValue(ctx, global);
66
67
  JS_FreeValue(ctx, strigified);
@@ -69,8 +70,16 @@ VALUE rb_module_eval_js_code(
69
70
  } else if (JS_VALUE_IS_NAN(res)) {
70
71
  result = ID2SYM(rb_intern(nanId));
71
72
  } else if (JS_IsNumber(res)) {
72
- JS_ToInt32(ctx, &r, res);
73
- result = INT2NUM(r);
73
+ int tag = JS_VALUE_GET_TAG(res);
74
+ if (JS_TAG_IS_FLOAT64(tag)) {
75
+ double double_res;
76
+ JS_ToFloat64(ctx, &double_res, res);
77
+ result = DBL2NUM(double_res);
78
+ } else {
79
+ int int_res;
80
+ JS_ToInt32(ctx, &int_res, res);
81
+ result = INT2NUM(int_res);
82
+ }
74
83
  } else if (JS_IsString(res)) {
75
84
  JSValue maybeString = JS_ToString(ctx, res);
76
85
  const char *msg = JS_ToCString(ctx, maybeString);
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quickjs
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/quickjs.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
3
4
  require_relative "quickjs/version"
4
5
  require_relative "quickjs/quickjsrb"
5
6
 
6
7
  module Quickjs
7
- FEATURE_STD = :std
8
- FEATURE_OS = :os
8
+ MODULE_STD = :std
9
+ MODULE_OS = :os
9
10
 
10
11
  def evalCode(
11
12
  code,
@@ -20,8 +21,8 @@ module Quickjs
20
21
  code,
21
22
  opts[:memoryLimit] || 1024 * 1024 * 128,
22
23
  opts[:maxStackSize] || 1024 * 1024 * 4,
23
- opts[:features].include?(Quickjs::FEATURE_STD),
24
- opts[:features].include?(Quickjs::FEATURE_OS),
24
+ opts[:features].include?(Quickjs::MODULE_STD),
25
+ opts[:features].include?(Quickjs::MODULE_OS),
25
26
  )
26
27
  end
27
28
  module_function :evalCode
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quickjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - hmsk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-14 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: A native wrapper to run QuickJS in Ruby
14
28
  email:
15
29
  - k.hamasaki@gmail.com
@@ -20,6 +34,7 @@ extra_rdoc_files: []
20
34
  files:
21
35
  - CHANGELOG.md
22
36
  - LICENSE
37
+ - README.md
23
38
  - Rakefile
24
39
  - ext/quickjsrb/extconf.rb
25
40
  - ext/quickjsrb/quickjs/LICENSE