wasval 0.1.0 → 0.1.1
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/README.md +30 -0
- data/lib/wasval/install/ruby_wasm.rb +50 -4
- data/lib/wasval/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: 93fa9232ed514267c694021eda5476997530d0e77d1a6dffaece2f151ea084f0
|
|
4
|
+
data.tar.gz: 588ddd64e4a7e5626cd7f930063e64552bb86fd311452ce330896243024c0d66
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1dbaef12759f4042ac20712caa799fff41569b8e8c63fb9578c22d63ccf7fb6fe98749477aea390e2d137626dc53a9d21e33501bde2bde795a4e87b3b93e5d0a
|
|
7
|
+
data.tar.gz: c12b33a60c9106651c4c790838b047a8f1a766edfad0f122a26f2fbd07dbe4f9c5c98d7f5e94e62a2c7f5d4d7b1530d4d1a23480b24cb2dfc95ede6e0bcfd065
|
data/README.md
CHANGED
|
@@ -50,6 +50,36 @@ You can customize the destination via the `WASVAL_RUBY_WASM_PATH` environment va
|
|
|
50
50
|
Wasval::Install::RubyWasm.new(dest: "/path/to/ruby.wasm").download
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
### Bundling gems into the binary (`include_gems`)
|
|
54
|
+
|
|
55
|
+
By default, the installed `ruby.wasm` binary does not include the standard library or gems, so `require` calls inside the sandbox will fail. To bundle the standard library (and any gems installed in the `usr` directory of the tarball) into the binary, use the `include_gems: true` option:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
Wasval::Install::RubyWasm.new(
|
|
59
|
+
include_gems: true,
|
|
60
|
+
pack_output: "/path/to/ruby-packed.wasm",
|
|
61
|
+
serialized_dest: "/path/to/ruby.cwasm"
|
|
62
|
+
).install
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
This extracts the `usr` directory from the downloaded tarball, packs it into the binary via `rbwasm pack`, and serializes the result. The packed binary is written to `pack_output` (defaults to `~/.wasval/ruby-packed.wasm`), and the serialized module is written to `serialized_dest`.
|
|
66
|
+
|
|
67
|
+
If you have a pre-existing `usr` directory (e.g. with additional gems installed), pass its path via `include_gems:`:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
Wasval::Install::RubyWasm.new(
|
|
71
|
+
include_gems: true,
|
|
72
|
+
include_gems: "/path/to/usr"
|
|
73
|
+
).install
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
After installing with `include_gems: true`, point `WASVAL_RUBY_CWASM_PATH` to the serialized binary and `require` will work inside the sandbox:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
result = Wasval.execute("require 'json'; JSON.parse('1')")
|
|
80
|
+
result.status # => :success
|
|
81
|
+
```
|
|
82
|
+
|
|
53
83
|
## Usage
|
|
54
84
|
|
|
55
85
|
Set the `WASVAL_RUBY_WASM_PATH` environment variable to point to the `ruby.wasm` binary, then execute Ruby code:
|
|
@@ -17,14 +17,19 @@ module Wasval
|
|
|
17
17
|
DEFAULT_INSTALL_DIR = File.expand_path("~/.wasval")
|
|
18
18
|
DEFAULT_BINARY_NAME = "ruby.wasm"
|
|
19
19
|
DEFAULT_SERIALIZED_BINARY_NAME = "ruby.cwasm"
|
|
20
|
+
DEFAULT_PACKED_BINARY_NAME = "ruby-packed.wasm"
|
|
21
|
+
DEFAULT_USR_DIR_NAME = "usr"
|
|
20
22
|
|
|
21
|
-
attr_reader :dest, :serialized_dest, :ruby_version, :profile
|
|
23
|
+
attr_reader :dest, :serialized_dest, :ruby_version, :profile, :pack_dirs, :pack_output, :include_gems
|
|
22
24
|
|
|
23
|
-
def initialize(dest: nil, serialized_dest: nil, ruby_version: nil, profile: :full)
|
|
25
|
+
def initialize(dest: nil, serialized_dest: nil, ruby_version: nil, profile: :full, pack_dirs: nil, pack_output: nil, include_gems: nil)
|
|
24
26
|
@ruby_version = ruby_version || ENV["WASVAL_RUBY_VERSION"] || default_ruby_version
|
|
25
27
|
@profile = profile.to_s
|
|
26
28
|
@dest = dest || ENV["WASVAL_RUBY_WASM_PATH"] || File.join(DEFAULT_INSTALL_DIR, DEFAULT_BINARY_NAME)
|
|
27
29
|
@serialized_dest = serialized_dest || ENV["WASVAL_RUBY_CWASM_PATH"] || File.join(File.dirname(@dest), DEFAULT_SERIALIZED_BINARY_NAME)
|
|
30
|
+
@pack_dirs = pack_dirs
|
|
31
|
+
@pack_output = pack_output || File.join(File.dirname(@dest), DEFAULT_PACKED_BINARY_NAME)
|
|
32
|
+
@include_gems = include_gems
|
|
28
33
|
end
|
|
29
34
|
|
|
30
35
|
def download
|
|
@@ -38,18 +43,49 @@ module Wasval
|
|
|
38
43
|
end
|
|
39
44
|
|
|
40
45
|
def install
|
|
41
|
-
|
|
46
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
|
47
|
+
Dir.mktmpdir do |tmpdir|
|
|
48
|
+
tarball_path = File.join(tmpdir, tarball_name)
|
|
49
|
+
download_file(download_url, tarball_path)
|
|
50
|
+
extract_binary(tarball_path)
|
|
51
|
+
if pack_dirs || include_gems
|
|
52
|
+
dirs = pack_dirs ? pack_dirs.dup : []
|
|
53
|
+
if include_gems
|
|
54
|
+
if include_gems == true
|
|
55
|
+
actual_usr_dir = File.join(tmpdir, DEFAULT_USR_DIR_NAME)
|
|
56
|
+
extract_usr_dir(tarball_path, actual_usr_dir)
|
|
57
|
+
else
|
|
58
|
+
actual_usr_dir = include_gems
|
|
59
|
+
end
|
|
60
|
+
dirs << actual_usr_dir
|
|
61
|
+
end
|
|
62
|
+
pack(*dirs, output: pack_output) if dirs.any?
|
|
63
|
+
end
|
|
64
|
+
end
|
|
42
65
|
serialize
|
|
43
66
|
end
|
|
44
67
|
|
|
45
68
|
def serialize
|
|
46
69
|
engine = Wasmtime::Engine.new(epoch_interruption: true)
|
|
47
|
-
|
|
70
|
+
source = (pack_dirs || include_gems) ? pack_output : dest
|
|
71
|
+
mod = Wasmtime::Module.from_file(engine, source)
|
|
48
72
|
FileUtils.mkdir_p(File.dirname(serialized_dest))
|
|
49
73
|
File.binwrite(serialized_dest, mod.serialize)
|
|
50
74
|
serialized_dest
|
|
51
75
|
end
|
|
52
76
|
|
|
77
|
+
def pack(*dirs, output:)
|
|
78
|
+
args = [dest]
|
|
79
|
+
dirs.each do |dir|
|
|
80
|
+
dir_arg = dir.include?("::") ? dir : "#{dir}::/#{File.basename(dir)}"
|
|
81
|
+
args.push("--dir", dir_arg)
|
|
82
|
+
end
|
|
83
|
+
args.push("-o", output)
|
|
84
|
+
|
|
85
|
+
system("rbwasm", "pack", *args, exception: true)
|
|
86
|
+
output
|
|
87
|
+
end
|
|
88
|
+
|
|
53
89
|
def installed?
|
|
54
90
|
File.exist?(dest)
|
|
55
91
|
end
|
|
@@ -88,6 +124,16 @@ module Wasval
|
|
|
88
124
|
end
|
|
89
125
|
end
|
|
90
126
|
|
|
127
|
+
def extract_usr_dir(tarball_path, dest_usr_dir)
|
|
128
|
+
Dir.mktmpdir do |extract_dir|
|
|
129
|
+
system("tar", "-xzf", tarball_path, "-C", extract_dir, exception: true)
|
|
130
|
+
src_usr = Dir.glob("#{extract_dir}/**/usr").min_by(&:length)
|
|
131
|
+
raise "usr directory not found in tarball" unless src_usr && File.directory?(src_usr)
|
|
132
|
+
FileUtils.cp_r(src_usr, File.dirname(dest_usr_dir))
|
|
133
|
+
end
|
|
134
|
+
dest_usr_dir
|
|
135
|
+
end
|
|
136
|
+
|
|
91
137
|
def extract_binary(tarball_path)
|
|
92
138
|
Zlib::GzipReader.open(tarball_path) do |gz|
|
|
93
139
|
Gem::Package::TarReader.new(gz) do |tar|
|
data/lib/wasval/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wasval
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yuji Yaginuma
|
|
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
72
72
|
- !ruby/object:Gem::Version
|
|
73
73
|
version: '0'
|
|
74
74
|
requirements: []
|
|
75
|
-
rubygems_version: 4.0.
|
|
75
|
+
rubygems_version: 4.0.6
|
|
76
76
|
specification_version: 4
|
|
77
77
|
summary: Execute 'eval' in a WASM runtime
|
|
78
78
|
test_files: []
|