opal-webassembly 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rspec-opal +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +25 -0
- data/LICENSE.adoc +31 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +45 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/opal-repl-wasm +4 -0
- data/bin/setup +8 -0
- data/examples/experiment/Makefile +2 -0
- data/examples/experiment/experiment-wasm.c +74 -0
- data/examples/experiment/experiment-wasm.wasm +0 -0
- data/examples/experiment/experiment.rb +25 -0
- data/examples/experiment/experiment.sh +2 -0
- data/examples/onigmo/onigmo-wasm.wasm +0 -0
- data/examples/onigmo/onigmo.rb +4 -0
- data/examples/onigmo/onigmo.sh +2 -0
- data/examples/simple_ffi/Rakefile +14 -0
- data/examples/simple_ffi/simple-wasm.wasm +0 -0
- data/examples/simple_ffi/simple-wasm.wat +8 -0
- data/examples/simple_ffi/simple.rb +14 -0
- data/exe/opal-repl-wasm +9 -0
- data/lib/opal/webassembly.rb +9 -0
- data/lib/opal/webassembly/processor.rb +49 -0
- data/lib/opal/webassembly/version.rb +5 -0
- data/opal-webassembly.gemspec +31 -0
- data/opal/ffi.rb +22 -0
- data/opal/ffi/library.rb +67 -0
- data/opal/ffi/pointer.rb +289 -0
- data/opal/ffi/struct.rb +125 -0
- data/opal/ffi/types.rb +226 -0
- data/opal/webassembly.rb +68 -0
- data/opal/webassembly/global.rb +15 -0
- data/opal/webassembly/instance.rb +44 -0
- data/opal/webassembly/memory.rb +18 -0
- data/opal/webassembly/module.rb +10 -0
- data/opal/webassembly/table.rb +35 -0
- data/spec-opal/ffi_spec.rb +139 -0
- data/spec-opal/spec_helper.rb +18 -0
- data/spec-opal/webassembly_spec.rb +12 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 777a9c8adbfc32598b84812cd1a761e23237fd8c79555f1ae6e7dc251cf7d73c
|
4
|
+
data.tar.gz: d2f49898aeffc50769fc5f3cd6c8b038ec771844d49530f568322ada6a8e56df
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 12db815d8bae8d32335b243d01ff677b47cc49d9dbfc648f0c26b5ad2b99706fc8277dfa5e801d8ee621bc271bfd0353f40867dae89a595675059b02a4866430
|
7
|
+
data.tar.gz: 364cbc01b4a86e4c82cc66af0f808c6b9dce6dce3f023a28bf154a380fa24af090b6cdd901c2f3c64aa5698df510b6081e9eaf2509172fad8ce98a411461b755
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rspec-opal
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in opal-webassembly.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem "rake", "~> 12.0"
|
7
|
+
gem "rspec", "~> 3.0"
|
8
|
+
|
9
|
+
gem "pry"
|
10
|
+
|
11
|
+
if File.directory? __dir__+"/../opal"
|
12
|
+
gem "opal", path: __dir__+"/../opal"
|
13
|
+
else
|
14
|
+
gem "opal"
|
15
|
+
end
|
16
|
+
|
17
|
+
gem "opal-rspec"
|
18
|
+
|
19
|
+
if File.directory? __dir__+"/../opal-sprockets"
|
20
|
+
gem "opal-sprockets", path: __dir__+"/../opal-sprockets"
|
21
|
+
else
|
22
|
+
gem "opal-sprockets"
|
23
|
+
end
|
24
|
+
|
25
|
+
gem "mini_racer"
|
data/LICENSE.adoc
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
= Licenses & Copyright
|
2
|
+
|
3
|
+
This license file adheres to the formatting guidelines of
|
4
|
+
https://github.com/nevir/readable-licenses[readable-licenses].
|
5
|
+
|
6
|
+
|
7
|
+
== Ribose BSD 2-Clause License
|
8
|
+
|
9
|
+
Copyright (c) 2019-, https://www.ribose.com[Ribose Inc].
|
10
|
+
All rights reserved.
|
11
|
+
|
12
|
+
Redistribution and use in source and binary forms, with or without modification,
|
13
|
+
are permitted provided that the following conditions are met:
|
14
|
+
|
15
|
+
1. Redistributions of source code must retain the above copyright notice,
|
16
|
+
this list of conditions and the following disclaimer.
|
17
|
+
|
18
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
19
|
+
this list of conditions and the following disclaimer in the documentation
|
20
|
+
and/or other materials provided with the distribution.
|
21
|
+
|
22
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
23
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
24
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
25
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
26
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
27
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
28
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
29
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
30
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
31
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.adoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
= Opal::Webassembly
|
2
|
+
|
3
|
+
WebAssembly support for Opal.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
[source,ruby]
|
10
|
+
----
|
11
|
+
gem 'opal-webassembly'
|
12
|
+
----
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
[source,sh]
|
17
|
+
----
|
18
|
+
$ bundle install
|
19
|
+
----
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
[source,sh]
|
24
|
+
----
|
25
|
+
$ gem install opal-onigmo
|
26
|
+
----
|
27
|
+
|
28
|
+
|
29
|
+
== Usage
|
30
|
+
|
31
|
+
Please see `examples/simple_ffi` for basic usage.
|
32
|
+
|
33
|
+
== Development
|
34
|
+
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to https://rubygems.org[rubygems.org].
|
38
|
+
|
39
|
+
== Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hmdne/opal-webassembly.
|
42
|
+
|
43
|
+
== License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the BSD 2-clause license.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require 'opal/rspec/rake_task'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
Opal::RSpec::RakeTask.new("spec-opal") do |server, task|
|
7
|
+
require 'opal/webassembly'
|
8
|
+
require 'opal/webassembly/processor'
|
9
|
+
Opal.append_path __dir__+"/examples/simple_ffi"
|
10
|
+
Opal.append_path __dir__+"/examples/experiment"
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => [:spec, "spec-opal"]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "opal/webassembly"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/opal-repl-wasm
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
void *retpointer(void *ptr) {
|
2
|
+
return ptr;
|
3
|
+
}
|
4
|
+
|
5
|
+
float retfloat(float f) {
|
6
|
+
return -f*2;
|
7
|
+
}
|
8
|
+
|
9
|
+
double retdouble(double f) {
|
10
|
+
return -f*2;
|
11
|
+
}
|
12
|
+
|
13
|
+
char *retstring(char *c) {
|
14
|
+
c[3] += 1;
|
15
|
+
c[5] += 1;
|
16
|
+
return c+2;
|
17
|
+
}
|
18
|
+
|
19
|
+
int longsize(void) { return sizeof(long); }
|
20
|
+
int longlongsize(void) { return sizeof(long long); }
|
21
|
+
int ptrsize(void) { return sizeof(void*); }
|
22
|
+
int floatsize(void) { return sizeof(float); }
|
23
|
+
int doublesize(void) { return sizeof(double); }
|
24
|
+
int longdoublesize(void) { return sizeof(long double); }
|
25
|
+
|
26
|
+
struct test_struct {
|
27
|
+
int a;
|
28
|
+
long long b;
|
29
|
+
char c;
|
30
|
+
int d;
|
31
|
+
char e;
|
32
|
+
void *f;
|
33
|
+
char g;
|
34
|
+
long double h;
|
35
|
+
} test_struct;
|
36
|
+
|
37
|
+
int structalignll(void) { return (long)&test_struct.b - (long)&test_struct.a; }
|
38
|
+
int structaligni(void) { return (long)&test_struct.d - (long)&test_struct.c; }
|
39
|
+
int structalignp(void) { return (long)&test_struct.f - (long)&test_struct.e; }
|
40
|
+
int structalignld(void) { return (long)&test_struct.h - (long)&test_struct.g; }
|
41
|
+
|
42
|
+
////// BASIC stdlib below:
|
43
|
+
extern unsigned char __heap_base;
|
44
|
+
|
45
|
+
unsigned int bump_pointer = (unsigned int)&__heap_base;
|
46
|
+
|
47
|
+
//// Adapted from: https://github.com/WebAssembly/wasi-libc / musl
|
48
|
+
void *memcpy(void *restrict dest, const void *restrict src, unsigned long n)
|
49
|
+
{
|
50
|
+
unsigned char *d = dest;
|
51
|
+
const unsigned char *s = src;
|
52
|
+
for (; n; n--) *d++ = *s++;
|
53
|
+
return dest;
|
54
|
+
}
|
55
|
+
|
56
|
+
// IT LEAKS BY DESIGN; adapted from: https://surma.dev/things/c-to-webassembly/
|
57
|
+
void* malloc(unsigned long n) {
|
58
|
+
unsigned int r = bump_pointer;
|
59
|
+
bump_pointer += 4 + ((n|3)+1);
|
60
|
+
unsigned long *ptr = (unsigned long *)r;
|
61
|
+
*ptr = n;
|
62
|
+
return (void *)(ptr + 1);
|
63
|
+
}
|
64
|
+
|
65
|
+
void free(void* p) {
|
66
|
+
// lol
|
67
|
+
}
|
68
|
+
|
69
|
+
void *realloc(void *ptr, unsigned long size) {
|
70
|
+
unsigned int cursize = *(int *)(ptr - 4);
|
71
|
+
void *newptr = malloc(size);
|
72
|
+
memcpy(newptr, ptr, cursize);
|
73
|
+
return newptr;
|
74
|
+
}
|
Binary file
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'experiment-wasm'
|
2
|
+
require 'ffi'
|
3
|
+
|
4
|
+
module Experiment
|
5
|
+
extend FFI::Library
|
6
|
+
|
7
|
+
ffi_lib "experiment-wasm"
|
8
|
+
|
9
|
+
attach_function :retpointer, [:pointer], :pointer
|
10
|
+
attach_function :retfloat, [:float], :float
|
11
|
+
attach_function :retdouble, [:double], :double
|
12
|
+
attach_function :retstring, [:string], :string
|
13
|
+
|
14
|
+
attach_function :longsize, [], :int
|
15
|
+
attach_function :longlongsize, [], :int
|
16
|
+
attach_function :ptrsize, [], :int
|
17
|
+
attach_function :floatsize, [], :int
|
18
|
+
attach_function :doublesize, [], :int
|
19
|
+
attach_function :longdoublesize, [], :int
|
20
|
+
|
21
|
+
attach_function :structalignld, [], :int
|
22
|
+
attach_function :structalignll, [], :int
|
23
|
+
attach_function :structaligni, [], :int
|
24
|
+
attach_function :structalignp, [], :int
|
25
|
+
end
|
Binary file
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH << __dir__+"/../../lib"
|
2
|
+
|
3
|
+
file "simple.out.js" => ["simple.rb"] do
|
4
|
+
require 'opal/builder'
|
5
|
+
require 'opal/webassembly'
|
6
|
+
require 'opal/webassembly/processor'
|
7
|
+
|
8
|
+
builder = Opal::Builder.new
|
9
|
+
builder.append_paths(__dir__)
|
10
|
+
out = builder.build("simple")
|
11
|
+
File.write("simple.out.js", out)
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => "simple.out.js"
|
Binary file
|
data/exe/opal-repl-wasm
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'opal/builder_processors'
|
3
|
+
|
4
|
+
module Opal
|
5
|
+
module BuilderProcessors
|
6
|
+
class WASMProcessor < Processor
|
7
|
+
handles :wasm
|
8
|
+
|
9
|
+
# Override the initialization, because we really don't want a new line mangling
|
10
|
+
def initialize(source, filename, options = {})
|
11
|
+
#source += "\n" unless source.end_with?("\n")
|
12
|
+
@source, @filename, @options = source, filename, options
|
13
|
+
@requires = []
|
14
|
+
@required_trees = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def source
|
18
|
+
module_name = ::Opal::Compiler.module_name(@filename)
|
19
|
+
source = Base64.strict_encode64(@source.to_s)
|
20
|
+
<<~END
|
21
|
+
Opal.modules[#{module_name.inspect}] = function() {
|
22
|
+
// WebAssembly module
|
23
|
+
var self = Opal.top;
|
24
|
+
self.$require("webassembly");
|
25
|
+
Opal.WebAssembly.$load_from_base64('#{source}', #{module_name.inspect});
|
26
|
+
};
|
27
|
+
END
|
28
|
+
end
|
29
|
+
|
30
|
+
# No source map support. Required to be mocked for tests.
|
31
|
+
def source_map
|
32
|
+
o = Object.new
|
33
|
+
def o.generated_code; ""; end
|
34
|
+
def o.to_h; {version: 3, sections: nil, sources: [], mappings: []}; end
|
35
|
+
o
|
36
|
+
end
|
37
|
+
|
38
|
+
def requires
|
39
|
+
['webassembly'] + super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module Opal
|
46
|
+
module WebAssembly
|
47
|
+
Processor = ::Opal::BuilderProcessors::WASMProcessor
|
48
|
+
end
|
49
|
+
end
|