wasmer 0.2.0 → 0.3.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.
data/src/lib.rs CHANGED
@@ -1,7 +1,8 @@
1
1
  #![deny(warnings)]
2
2
 
3
- use rutie::{Class, Object};
3
+ use rutie::{Class, Module, Object};
4
4
 
5
+ pub mod error;
5
6
  pub mod instance;
6
7
  pub mod memory;
7
8
  pub mod module;
@@ -9,83 +10,108 @@ pub mod module;
9
10
  #[allow(non_snake_case)]
10
11
  #[no_mangle]
11
12
  pub extern "C" fn Init_wasmer() {
13
+ let mut wasmer_module = Module::from_existing("Wasmer");
14
+
12
15
  let instance_data_class = Class::from_existing("Object");
13
16
 
14
17
  // Declare the `Instance` Ruby class.
15
- Class::new("Instance", Some(&instance_data_class)).define(|itself| {
16
- // Declare the `self.new` method.
17
- itself.def_self("new", instance::ruby_instance_new);
18
+ wasmer_module
19
+ .define_nested_class("Instance", Some(&instance_data_class))
20
+ .define(|itself| {
21
+ // Declare the `self.new` method.
22
+ itself.def_self("new", instance::ruby_instance_new);
18
23
 
19
- // Declare the `exports` getter method.
20
- itself.def("exports", instance::ruby_instance_exported_functions);
24
+ // Declare the `exports` getter method.
25
+ itself.def("exports", instance::ruby_instance_exported_functions);
21
26
 
22
- // Declare the `memory` getter method.
23
- itself.def("memory", instance::ruby_instance_memory);
24
- });
27
+ // Declare the `memory` getter method.
28
+ itself.def("memory", instance::ruby_instance_memory);
29
+ });
25
30
 
26
31
  let exported_functions_data_class = Class::from_existing("Object");
27
32
 
28
33
  // Declare the `ExportedFunctions` Ruby class.
29
- Class::new("ExportedFunctions", Some(&exported_functions_data_class)).define(|itself| {
30
- // Declare the `method_missing` method.
31
- itself.def(
32
- "method_missing",
33
- instance::ruby_exported_functions_method_missing,
34
- );
35
- });
34
+ wasmer_module
35
+ .define_nested_class("ExportedFunctions", Some(&exported_functions_data_class))
36
+ .define(|itself| {
37
+ // Declare the `respond_to_missing?` method.
38
+ itself.def(
39
+ "respond_to_missing?",
40
+ instance::ruby_exported_functions_method_exists,
41
+ );
42
+
43
+ // Declare the `method_missing` method.
44
+ itself.def(
45
+ "method_missing",
46
+ instance::ruby_exported_functions_method_missing,
47
+ );
48
+ });
36
49
 
37
50
  let module_data_class = Class::from_existing("Object");
38
51
 
39
52
  // Declare the `Module` Ruby class.
40
- Class::new("Module", Some(&module_data_class)).define(|itself| {
41
- // Declare the `self.validate` method.
42
- itself.def_self("validate", module::ruby_module_validate);
43
- });
53
+ wasmer_module
54
+ .define_nested_class("Module", Some(&module_data_class))
55
+ .define(|itself| {
56
+ // Declare the `self.validate` method.
57
+ itself.def_self("validate", module::ruby_module_validate);
58
+ });
44
59
 
45
60
  let memory_data_class = Class::from_existing("Object");
46
61
 
47
62
  // Declare the `Memory` Ruby class.
48
- Class::new("Memory", Some(&memory_data_class)).define(|itself| {
49
- // Declare the `view` method.
50
- itself.def("uint8_view", memory::ruby_memory_uint8array);
63
+ wasmer_module
64
+ .define_nested_class("Memory", Some(&memory_data_class))
65
+ .define(|itself| {
66
+ // Declare the `grow` method.
67
+ itself.def("grow", memory::ruby_memory_grow);
68
+
69
+ // Declare the `view` method.
70
+ itself.def("uint8_view", memory::ruby_memory_uint8array);
51
71
 
52
- // Declare the `view` method.
53
- itself.def("int8_view", memory::ruby_memory_int8array);
72
+ // Declare the `view` method.
73
+ itself.def("int8_view", memory::ruby_memory_int8array);
54
74
 
55
- // Declare the `view` method.
56
- itself.def("uint16_view", memory::ruby_memory_uint16array);
75
+ // Declare the `view` method.
76
+ itself.def("uint16_view", memory::ruby_memory_uint16array);
57
77
 
58
- // Declare the `view` method.
59
- itself.def("int16_view", memory::ruby_memory_int16array);
78
+ // Declare the `view` method.
79
+ itself.def("int16_view", memory::ruby_memory_int16array);
60
80
 
61
- // Declare the `view` method.
62
- itself.def("uint32_view", memory::ruby_memory_uint32array);
81
+ // Declare the `view` method.
82
+ itself.def("uint32_view", memory::ruby_memory_uint32array);
63
83
 
64
- // Declare the `view` method.
65
- itself.def("int32_view", memory::ruby_memory_int32array);
66
- });
84
+ // Declare the `view` method.
85
+ itself.def("int32_view", memory::ruby_memory_int32array);
86
+ });
67
87
 
68
88
  macro_rules! memory_view {
69
89
  ($class_name:ident in $mod_name:ident) => {
70
90
  let uint8array_data_class = Class::from_existing("Object");
71
91
 
72
92
  // Declare the `MemoryView` Ruby class.
73
- Class::new(stringify!($class_name), Some(&uint8array_data_class)).define(|itself| {
74
- // Declare the `bytes_per_element` getter method.
75
- itself.def(
76
- "bytes_per_element",
77
- memory::view::$mod_name::ruby_memory_view_bytes_per_element,
78
- );
79
-
80
- // Declare the `length` method.
81
- itself.def("length", memory::view::$mod_name::ruby_memory_view_length);
82
-
83
- // Declare the `[]=` (set) method.
84
- itself.def("[]=", memory::view::$mod_name::ruby_memory_view_set);
85
-
86
- // Declare the `[]` (get) method.
87
- itself.def("[]", memory::view::$mod_name::ruby_memory_view_get);
88
- });
93
+ wasmer_module
94
+ .define_nested_class(stringify!($class_name), Some(&uint8array_data_class))
95
+ .define(|itself| {
96
+ // Declare the `bytes_per_element` getter method.
97
+ itself.def(
98
+ "bytes_per_element",
99
+ memory::view::$mod_name::ruby_memory_view_bytes_per_element,
100
+ );
101
+
102
+ // Declare the `length` method.
103
+ itself.def("length", memory::view::$mod_name::ruby_memory_view_length);
104
+
105
+ // Declare the `[]=` (set) method.
106
+ itself.def("[]=", memory::view::$mod_name::ruby_memory_view_set);
107
+
108
+ // Declare the `[]` (get) method.
109
+ itself.def("[]", memory::view::$mod_name::ruby_memory_view_get);
110
+
111
+ // Declare the `each` method.
112
+ itself.def("each", memory::view::$mod_name::ruby_memory_view_each);
113
+ })
114
+ .include("Enumerable");
89
115
  };
90
116
  }
91
117
 
@@ -2,18 +2,26 @@
2
2
 
3
3
  pub mod view;
4
4
 
5
- use crate::memory::view::{
6
- int16array::{RubyMemoryView as RubyInt16Array, MEMORY_VIEW_WRAPPER as INT16ARRAY_WRAPPER},
7
- int32array::{RubyMemoryView as RubyInt32Array, MEMORY_VIEW_WRAPPER as INT32ARRAY_WRAPPER},
8
- int8array::{RubyMemoryView as RubyInt8Array, MEMORY_VIEW_WRAPPER as INT8ARRAY_WRAPPER},
9
- uint16array::{RubyMemoryView as RubyUint16Array, MEMORY_VIEW_WRAPPER as UINT16ARRAY_WRAPPER},
10
- uint32array::{RubyMemoryView as RubyUint32Array, MEMORY_VIEW_WRAPPER as UINT32ARRAY_WRAPPER},
11
- uint8array::{RubyMemoryView as RubyUint8Array, MEMORY_VIEW_WRAPPER as UINT8ARRAY_WRAPPER},
5
+ use crate::{
6
+ error::unwrap_or_raise,
7
+ memory::view::{
8
+ int16array::{RubyMemoryView as RubyInt16Array, MEMORY_VIEW_WRAPPER as INT16ARRAY_WRAPPER},
9
+ int32array::{RubyMemoryView as RubyInt32Array, MEMORY_VIEW_WRAPPER as INT32ARRAY_WRAPPER},
10
+ int8array::{RubyMemoryView as RubyInt8Array, MEMORY_VIEW_WRAPPER as INT8ARRAY_WRAPPER},
11
+ uint16array::{
12
+ RubyMemoryView as RubyUint16Array, MEMORY_VIEW_WRAPPER as UINT16ARRAY_WRAPPER,
13
+ },
14
+ uint32array::{
15
+ RubyMemoryView as RubyUint32Array, MEMORY_VIEW_WRAPPER as UINT32ARRAY_WRAPPER,
16
+ },
17
+ uint8array::{RubyMemoryView as RubyUint8Array, MEMORY_VIEW_WRAPPER as UINT8ARRAY_WRAPPER},
18
+ },
12
19
  };
13
20
  use lazy_static::lazy_static;
14
- use rutie::{class, methods, wrappable_struct, Class, Integer, Object};
21
+ use rutie::{class, methods, wrappable_struct, AnyException, Exception, Integer, Module, Object};
15
22
  use std::rc::Rc;
16
23
  use wasmer_runtime as runtime;
24
+ use wasmer_runtime_core::units::Pages;
17
25
 
18
26
  pub struct Memory {
19
27
  memory: Rc<runtime::Memory>,
@@ -47,6 +55,18 @@ impl Memory {
47
55
  pub fn int32_view(&self, offset: usize) -> view::int32array::MemoryView {
48
56
  view::int32array::MemoryView::new(self.memory.clone(), offset)
49
57
  }
58
+
59
+ pub fn grow(&self, number_of_pages: u32) -> Result<u32, AnyException> {
60
+ self.memory
61
+ .grow(Pages(number_of_pages))
62
+ .map(|previous_pages| previous_pages.0)
63
+ .map_err(|err| {
64
+ AnyException::new(
65
+ "RuntimeError",
66
+ Some(&format!("Failed to grow the memory: {}.", err)),
67
+ )
68
+ })
69
+ }
50
70
  }
51
71
 
52
72
  wrappable_struct!(Memory, MemoryWrapper, MEMORY_WRAPPER);
@@ -57,64 +77,83 @@ class!(RubyMemory);
57
77
  methods!(
58
78
  RubyMemory,
59
79
  itself,
60
-
61
80
  // Glue code to call the `Memory.uint8_view` method.
62
81
  fn ruby_memory_uint8array(offset: Integer) -> RubyUint8Array {
63
- let offset = offset
64
- .map(|offset| offset.to_i64() as usize)
65
- .unwrap_or(0);
82
+ let offset = offset.map(|offset| offset.to_i64() as usize).unwrap_or(0);
66
83
  let memory_view = itself.get_data(&*MEMORY_WRAPPER).uint8_view(offset);
67
84
 
68
- Class::from_existing("Uint8Array").wrap_data(memory_view, &*UINT8ARRAY_WRAPPER)
85
+ let wasmer_module = Module::from_existing("Wasmer");
86
+ wasmer_module
87
+ .get_nested_class("Uint8Array")
88
+ .wrap_data(memory_view, &*UINT8ARRAY_WRAPPER)
69
89
  }
70
90
 
71
91
  // Glue code to call the `Memory.int8_view` method.
72
92
  fn ruby_memory_int8array(offset: Integer) -> RubyInt8Array {
73
- let offset = offset
74
- .map(|offset| offset.to_i64() as usize)
75
- .unwrap_or(0);
93
+ let offset = offset.map(|offset| offset.to_i64() as usize).unwrap_or(0);
76
94
  let memory_view = itself.get_data(&*MEMORY_WRAPPER).int8_view(offset);
77
95
 
78
- Class::from_existing("Int8Array").wrap_data(memory_view, &*INT8ARRAY_WRAPPER)
96
+ let wasmer_module = Module::from_existing("Wasmer");
97
+ wasmer_module
98
+ .get_nested_class("Int8Array")
99
+ .wrap_data(memory_view, &*INT8ARRAY_WRAPPER)
79
100
  }
80
101
 
81
102
  // Glue code to call the `Memory.uint16_view` method.
82
103
  fn ruby_memory_uint16array(offset: Integer) -> RubyUint16Array {
83
- let offset = offset
84
- .map(|offset| offset.to_i64() as usize)
85
- .unwrap_or(0);
104
+ let offset = offset.map(|offset| offset.to_i64() as usize).unwrap_or(0);
86
105
  let memory_view = itself.get_data(&*MEMORY_WRAPPER).uint16_view(offset);
87
106
 
88
- Class::from_existing("Uint16Array").wrap_data(memory_view, &*UINT16ARRAY_WRAPPER)
107
+ let wasmer_module = Module::from_existing("Wasmer");
108
+ wasmer_module
109
+ .get_nested_class("Uint16Array")
110
+ .wrap_data(memory_view, &*UINT16ARRAY_WRAPPER)
89
111
  }
90
112
 
91
113
  // Glue code to call the `Memory.int16_view` method.
92
114
  fn ruby_memory_int16array(offset: Integer) -> RubyInt16Array {
93
- let offset = offset
94
- .map(|offset| offset.to_i64() as usize)
95
- .unwrap_or(0);
115
+ let offset = offset.map(|offset| offset.to_i64() as usize).unwrap_or(0);
96
116
  let memory_view = itself.get_data(&*MEMORY_WRAPPER).int16_view(offset);
97
117
 
98
- Class::from_existing("Int16Array").wrap_data(memory_view, &*INT16ARRAY_WRAPPER)
118
+ let wasmer_module = Module::from_existing("Wasmer");
119
+ wasmer_module
120
+ .get_nested_class("Int16Array")
121
+ .wrap_data(memory_view, &*INT16ARRAY_WRAPPER)
99
122
  }
100
123
 
101
124
  // Glue code to call the `Memory.uint32_view` method.
102
125
  fn ruby_memory_uint32array(offset: Integer) -> RubyUint32Array {
103
- let offset = offset
104
- .map(|offset| offset.to_i64() as usize)
105
- .unwrap_or(0);
126
+ let offset = offset.map(|offset| offset.to_i64() as usize).unwrap_or(0);
106
127
  let memory_view = itself.get_data(&*MEMORY_WRAPPER).uint32_view(offset);
107
128
 
108
- Class::from_existing("Uint32Array").wrap_data(memory_view, &*UINT32ARRAY_WRAPPER)
129
+ let wasmer_module = Module::from_existing("Wasmer");
130
+ wasmer_module
131
+ .get_nested_class("Uint32Array")
132
+ .wrap_data(memory_view, &*UINT32ARRAY_WRAPPER)
109
133
  }
110
134
 
111
135
  // Glue code to call the `Memory.int32_view` method.
112
136
  fn ruby_memory_int32array(offset: Integer) -> RubyInt32Array {
113
- let offset = offset
114
- .map(|offset| offset.to_i64() as usize)
115
- .unwrap_or(0);
137
+ let offset = offset.map(|offset| offset.to_i64() as usize).unwrap_or(0);
116
138
  let memory_view = itself.get_data(&*MEMORY_WRAPPER).int32_view(offset);
117
139
 
118
- Class::from_existing("Int32Array").wrap_data(memory_view, &*INT32ARRAY_WRAPPER)
140
+ let wasmer_module = Module::from_existing("Wasmer");
141
+ wasmer_module
142
+ .get_nested_class("Int32Array")
143
+ .wrap_data(memory_view, &*INT32ARRAY_WRAPPER)
144
+ }
145
+
146
+ // Glue code to call the `Memory.grow` method.
147
+ fn ruby_memory_grow(number_of_pages: Integer) -> Integer {
148
+ let number_of_pages = number_of_pages
149
+ .map(|number_of_pages| number_of_pages.to_i32() as u32)
150
+ .unwrap_or(1);
151
+
152
+ unwrap_or_raise(|| {
153
+ itself
154
+ .get_data(&*MEMORY_WRAPPER)
155
+ .grow(number_of_pages)
156
+ .map(|previous_number_of_pages| Integer::new(previous_number_of_pages as i64))
157
+ })
119
158
  }
120
159
  );
@@ -1,8 +1,11 @@
1
1
  //! The `TypedArray`/`MemoryView` WebAssembly classes.
2
2
 
3
+ #[rustfmt::skip]
4
+
3
5
  macro_rules! memory_view {
4
6
  ($mod_name:ident over $wasm_type:ty | $bytes_per_element:expr) => {
5
7
  pub mod $mod_name {
8
+ use crate::error::unwrap_or_raise;
6
9
  use lazy_static::lazy_static;
7
10
  use rutie::{
8
11
  class, methods, wrappable_struct, AnyException, Exception, Fixnum, Integer,
@@ -68,13 +71,21 @@ macro_rules! memory_view {
68
71
  Ok(view[offset + index].get())
69
72
  }
70
73
  }
74
+
75
+ pub fn each(&self) {
76
+ let view = self.memory.view::<$wasm_type>();
77
+
78
+ for nth in self.offset..view.len() {
79
+ let value = view[nth].get() as i64;
80
+ VM::yield_object(Integer::from(value));
81
+ }
82
+ }
71
83
  }
72
84
 
73
85
  wrappable_struct!(MemoryView, MemoryViewWrapper, MEMORY_VIEW_WRAPPER);
74
86
 
75
87
  class!(RubyMemoryView);
76
88
 
77
- #[rustfmt::skip]
78
89
  methods!(
79
90
  RubyMemoryView,
80
91
  _itself,
@@ -91,28 +102,35 @@ macro_rules! memory_view {
91
102
 
92
103
  // Glue code to call the `TypedArray.set` method.
93
104
  fn ruby_memory_view_set(index: Integer, value: Integer) -> NilClass {
94
- let memory_view = _itself.get_data(&*MEMORY_VIEW_WRAPPER);
95
- memory_view
96
- .set(
97
- index.map_err(|e| VM::raise_ex(e)).unwrap().to_i32() as isize,
98
- value.map_err(|e| VM::raise_ex(e)).unwrap().to_i32() as $wasm_type,
99
- )
100
- .map_err(|e| VM::raise_ex(AnyException::new("ArgumentError", Some(&e))))
101
- .unwrap();
102
-
103
- NilClass::new()
105
+ unwrap_or_raise(|| {
106
+ let memory_view = _itself.get_data(&*MEMORY_VIEW_WRAPPER);
107
+
108
+ memory_view
109
+ .set(index?.to_i32() as isize, value?.to_i32() as $wasm_type)
110
+ .map_err(|e| AnyException::new("ArgumentError", Some(&e)))?;
111
+
112
+ Ok(NilClass::new())
113
+ })
104
114
  }
105
115
 
106
116
  // Glue code to call the `TypedArray.get` method.
107
117
  fn ruby_memory_view_get(index: Integer) -> Fixnum {
108
- let memory_view = _itself.get_data(&*MEMORY_VIEW_WRAPPER);
118
+ unwrap_or_raise(|| {
119
+ let memory_view = _itself.get_data(&*MEMORY_VIEW_WRAPPER);
120
+
121
+ Ok(Fixnum::new(
122
+ memory_view
123
+ .get(index?.to_i32() as isize)
124
+ .map_err(|e| AnyException::new("ArgumentError", Some(&e)))?
125
+ as i64,
126
+ ))
127
+ })
128
+ }
109
129
 
110
- Fixnum::new(
111
- memory_view
112
- .get(index.map_err(|e| VM::raise_ex(e)).unwrap().to_i32() as isize)
113
- .map_err(|e| VM::raise_ex(AnyException::new("ArgumentError", Some(&e))))
114
- .unwrap() as i64,
115
- )
130
+ fn ruby_memory_view_each() -> RubyMemoryView {
131
+ let memory_view = _itself.get_data(&*MEMORY_VIEW_WRAPPER);
132
+ memory_view.each();
133
+ _itself
116
134
  }
117
135
  );
118
136
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wasmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Enderlin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-01 00:00:00.000000000 Z
11
+ date: 2019-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rutie
@@ -85,9 +85,11 @@ files:
85
85
  - Gemfile
86
86
  - README.md
87
87
  - Rakefile
88
+ - bors.toml
88
89
  - justfile
89
90
  - lib/wasmer.rb
90
91
  - lib/wasmer/version.rb
92
+ - src/error.rs
91
93
  - src/instance.rs
92
94
  - src/lib.rs
93
95
  - src/memory/mod.rs