janet_sandbox 0.1.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +316 -0
- data/examples/dsls/form_helpers.janet +90 -0
- data/examples/dsls/math_helpers.janet +71 -0
- data/examples/dsls/validation_helpers.janet +86 -0
- data/examples/initializers/janet_sandbox.rb +74 -0
- data/lib/janet_sandbox/builtin_dsls.rb +38 -0
- data/lib/janet_sandbox/configuration.rb +158 -0
- data/lib/janet_sandbox/engine.rb +55 -0
- data/lib/janet_sandbox/errors.rb +42 -0
- data/lib/janet_sandbox/rails/railtie.rb +37 -0
- data/lib/janet_sandbox/rails/script_concern.rb +78 -0
- data/lib/janet_sandbox/result.rb +38 -0
- data/lib/janet_sandbox/sandbox.rb +202 -0
- data/lib/janet_sandbox/version.rb +5 -0
- data/lib/janet_sandbox.rb +114 -0
- data/wasm/janet.wasm +0 -0
- metadata +126 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "wasmtime"
|
|
5
|
+
|
|
6
|
+
require_relative "janet_sandbox/version"
|
|
7
|
+
require_relative "janet_sandbox/errors"
|
|
8
|
+
require_relative "janet_sandbox/configuration"
|
|
9
|
+
require_relative "janet_sandbox/result"
|
|
10
|
+
require_relative "janet_sandbox/engine"
|
|
11
|
+
require_relative "janet_sandbox/sandbox"
|
|
12
|
+
require_relative "janet_sandbox/builtin_dsls"
|
|
13
|
+
|
|
14
|
+
module JanetSandbox
|
|
15
|
+
class << self
|
|
16
|
+
def configuration
|
|
17
|
+
@configuration ||= Configuration.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Configure the gem:
|
|
21
|
+
#
|
|
22
|
+
# JanetSandbox.configure do |config|
|
|
23
|
+
# config.fuel_limit = 200_000
|
|
24
|
+
#
|
|
25
|
+
# # Register custom DSLs
|
|
26
|
+
# config.register_dsl(:form_helpers, <<~'JANET')
|
|
27
|
+
# (defn get-field [name] (get-in ctx [:values (keyword name)]))
|
|
28
|
+
# JANET
|
|
29
|
+
#
|
|
30
|
+
# # Enable DSLs by default
|
|
31
|
+
# config.enable_dsl(:form_helpers)
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# @yield [Configuration]
|
|
35
|
+
# @return [Configuration]
|
|
36
|
+
def configure
|
|
37
|
+
yield(configuration) if block_given?
|
|
38
|
+
configuration
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Reset configuration to defaults (useful for testing).
|
|
42
|
+
#
|
|
43
|
+
# @return [Configuration]
|
|
44
|
+
def reset_configuration!
|
|
45
|
+
@configuration = Configuration.new
|
|
46
|
+
@engine = nil
|
|
47
|
+
@configuration
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Global engine instance (lazy, thread-safe).
|
|
51
|
+
#
|
|
52
|
+
# @return [Engine]
|
|
53
|
+
def engine
|
|
54
|
+
@engine || @mu.synchronize { @engine ||= Engine.new }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Quick evaluation using the global engine.
|
|
58
|
+
#
|
|
59
|
+
# @param source [String] Janet source code
|
|
60
|
+
# @param context [Hash] Context passed to the script as `ctx`
|
|
61
|
+
# @param dsls [Array<Symbol>, nil] DSLs to inject (nil uses defaults)
|
|
62
|
+
# @param opts [Hash] Additional options passed to sandbox
|
|
63
|
+
# @return [Result]
|
|
64
|
+
#
|
|
65
|
+
# @example
|
|
66
|
+
# result = JanetSandbox.evaluate(
|
|
67
|
+
# source: '(+ 1 2)',
|
|
68
|
+
# context: { values: { x: 10 } }
|
|
69
|
+
# )
|
|
70
|
+
# result.raw #=> 3
|
|
71
|
+
#
|
|
72
|
+
# @example With DSLs
|
|
73
|
+
# result = JanetSandbox.evaluate(
|
|
74
|
+
# source: '(get-field "name")',
|
|
75
|
+
# context: { values: { name: "Alice" } },
|
|
76
|
+
# dsls: [:form_helpers]
|
|
77
|
+
# )
|
|
78
|
+
def evaluate(source:, context: {}, dsls: nil, **opts)
|
|
79
|
+
engine.evaluate(source: source, context: context, dsls: dsls, **opts)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Register a DSL module (convenience method).
|
|
83
|
+
#
|
|
84
|
+
# @param name [Symbol, String] DSL name
|
|
85
|
+
# @param janet_source [String] Janet source code
|
|
86
|
+
# @param description [String] Optional description
|
|
87
|
+
# @return [Configuration]
|
|
88
|
+
def register_dsl(name, janet_source, description: nil)
|
|
89
|
+
configuration.register_dsl(name, janet_source, description: description)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Check if a DSL is registered.
|
|
93
|
+
#
|
|
94
|
+
# @param name [Symbol, String] DSL name
|
|
95
|
+
# @return [Boolean]
|
|
96
|
+
def dsl_registered?(name)
|
|
97
|
+
configuration.dsl_registered?(name)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# List all registered DSL names.
|
|
101
|
+
#
|
|
102
|
+
# @return [Array<Symbol>]
|
|
103
|
+
def dsl_names
|
|
104
|
+
configuration.dsl_names
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
@mu = Mutex.new
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Load Rails integration if Rails is present
|
|
112
|
+
if defined?(Rails::Railtie)
|
|
113
|
+
require_relative "janet_sandbox/rails/railtie"
|
|
114
|
+
end
|
data/wasm/janet.wasm
ADDED
|
Binary file
|
metadata
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: janet_sandbox
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Michael Harris
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: wasmtime
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '28.0'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '43.0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '28.0'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '43.0'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: rspec
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - "~>"
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '3.12'
|
|
39
|
+
type: :development
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - "~>"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '3.12'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: rake
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - "~>"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '13.0'
|
|
53
|
+
type: :development
|
|
54
|
+
prerelease: false
|
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '13.0'
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: rubocop
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - "~>"
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '1.50'
|
|
67
|
+
type: :development
|
|
68
|
+
prerelease: false
|
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - "~>"
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '1.50'
|
|
74
|
+
description: |
|
|
75
|
+
Embed the Janet programming language in Ruby apps via WebAssembly.
|
|
76
|
+
Safely execute user-authored scripts with configurable
|
|
77
|
+
resource limits, and pluggable DSL modules. Ideal for business logic,
|
|
78
|
+
dynamic rules, and user-extensible workflows.
|
|
79
|
+
email:
|
|
80
|
+
- osiris2918@gmail.com
|
|
81
|
+
executables: []
|
|
82
|
+
extensions: []
|
|
83
|
+
extra_rdoc_files: []
|
|
84
|
+
files:
|
|
85
|
+
- LICENSE.txt
|
|
86
|
+
- README.md
|
|
87
|
+
- examples/dsls/form_helpers.janet
|
|
88
|
+
- examples/dsls/math_helpers.janet
|
|
89
|
+
- examples/dsls/validation_helpers.janet
|
|
90
|
+
- examples/initializers/janet_sandbox.rb
|
|
91
|
+
- lib/janet_sandbox.rb
|
|
92
|
+
- lib/janet_sandbox/builtin_dsls.rb
|
|
93
|
+
- lib/janet_sandbox/configuration.rb
|
|
94
|
+
- lib/janet_sandbox/engine.rb
|
|
95
|
+
- lib/janet_sandbox/errors.rb
|
|
96
|
+
- lib/janet_sandbox/rails/railtie.rb
|
|
97
|
+
- lib/janet_sandbox/rails/script_concern.rb
|
|
98
|
+
- lib/janet_sandbox/result.rb
|
|
99
|
+
- lib/janet_sandbox/sandbox.rb
|
|
100
|
+
- lib/janet_sandbox/version.rb
|
|
101
|
+
- wasm/janet.wasm
|
|
102
|
+
homepage: https://github.com/mlh758/janet_sandbox
|
|
103
|
+
licenses:
|
|
104
|
+
- MIT
|
|
105
|
+
metadata:
|
|
106
|
+
homepage_uri: https://github.com/mlh758/janet_sandbox
|
|
107
|
+
source_code_uri: https://github.com/mlh758/janet_sandbox
|
|
108
|
+
changelog_uri: https://github.com/mlh758/janet_sandbox/blob/main/CHANGELOG.md
|
|
109
|
+
rdoc_options: []
|
|
110
|
+
require_paths:
|
|
111
|
+
- lib
|
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: 3.1.0
|
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
|
+
requirements:
|
|
119
|
+
- - ">="
|
|
120
|
+
- !ruby/object:Gem::Version
|
|
121
|
+
version: '0'
|
|
122
|
+
requirements: []
|
|
123
|
+
rubygems_version: 3.7.2
|
|
124
|
+
specification_version: 4
|
|
125
|
+
summary: Sandboxed Janet scripting for Ruby applications
|
|
126
|
+
test_files: []
|