| 1 |
const wasm = require('wasm-bindgen-test.js'); |
| 2 |
const assert = require('assert'); |
| 3 |
|
| 4 |
exports.js_simple = () => { |
| 5 |
const r = new wasm.ClassesSimple(); |
| 6 |
assert.strictEqual(r.add(0), 0); |
| 7 |
assert.strictEqual(r.add(1), 1); |
| 8 |
assert.strictEqual(r.add(1), 2); |
| 9 |
r.add(2); |
| 10 |
assert.strictEqual(r.consume(), 4); |
| 11 |
assert.throws(() => r.free(), /null pointer passed to rust/); |
| 12 |
|
| 13 |
const r2 = wasm.ClassesSimple.with_contents(10); |
| 14 |
assert.strictEqual(r2.add(1), 11); |
| 15 |
assert.strictEqual(r2.add(2), 13); |
| 16 |
assert.strictEqual(r2.add(3), 16); |
| 17 |
r2.free(); |
| 18 |
|
| 19 |
const r3 = new wasm.ClassesSimple(); |
| 20 |
assert.strictEqual(r3.add(42), 42); |
| 21 |
r3.free(); |
| 22 |
}; |
| 23 |
|
| 24 |
exports.js_strings = () => { |
| 25 |
const r = wasm.ClassesStrings1.new(); |
| 26 |
r.set(3); |
| 27 |
let bar = r.bar('baz'); |
| 28 |
r.free(); |
| 29 |
assert.strictEqual(bar.name(), 'foo-baz-3'); |
| 30 |
bar.free(); |
| 31 |
}; |
| 32 |
|
| 33 |
exports.js_exceptions = () => { |
| 34 |
// this test only works when `--debug` is passed to `wasm-bindgen` (or the |
| 35 |
// equivalent thereof) |
| 36 |
if (require('process').env.WASM_BINDGEN_NO_DEBUG) |
| 37 |
return; |
| 38 |
assert.throws(() => new wasm.ClassesExceptions1(), /cannot invoke `new` directly/); |
| 39 |
let a = wasm.ClassesExceptions1.new(); |
| 40 |
a.free(); |
| 41 |
assert.throws(() => a.free(), /null pointer passed to rust/); |
| 42 |
|
| 43 |
let b = wasm.ClassesExceptions1.new(); |
| 44 |
b.foo(b); |
| 45 |
assert.throws(() => b.bar(b), /recursive use of an object/); |
| 46 |
// TODO: throws because it tries to borrow_mut, but the throw_str from the previous line doesn't clean up the |
| 47 |
// RefMut so the object is left in a broken state. |
| 48 |
// We still try to call free here so the object is removed from the FinalizationRegistry when weak refs are enabled. |
| 49 |
assert.throws(() => b.free(), /recursive use of an object/); |
| 50 |
|
| 51 |
let c = wasm.ClassesExceptions1.new(); |
| 52 |
let d = wasm.ClassesExceptions2.new(); |
| 53 |
assert.throws(() => c.foo(d), /expected instance of ClassesExceptions1/); |
| 54 |
d.free(); |
| 55 |
c.free(); |
| 56 |
}; |
| 57 |
|
| 58 |
exports.js_pass_one_to_another = () => { |
| 59 |
let a = wasm.ClassesPassA.new(); |
| 60 |
let b = wasm.ClassesPassB.new(); |
| 61 |
a.foo(b); |
| 62 |
a.bar(b); |
| 63 |
a.free(); |
| 64 |
}; |
| 65 |
|
| 66 |
exports.take_class = foo => { |
| 67 |
assert.strictEqual(foo.inner(), 13); |
| 68 |
foo.free(); |
| 69 |
assert.throws(() => foo.free(), /null pointer passed to rust/); |
| 70 |
}; |
| 71 |
|
| 72 |
exports.js_constructors = () => { |
| 73 |
const foo = new wasm.ConstructorsFoo(1); |
| 74 |
assert.strictEqual(foo.get_number(), 1); |
| 75 |
foo.free(); |
| 76 |
|
| 77 |
assert.strictEqual(wasm.ConstructorsBar.new, undefined); |
| 78 |
const foo2 = new wasm.ConstructorsFoo(2); |
| 79 |
assert.strictEqual(foo2.get_number(), 2); |
| 80 |
foo2.free(); |
| 81 |
|
| 82 |
const bar = new wasm.ConstructorsBar(3, 4); |
| 83 |
assert.strictEqual(bar.get_sum(), 7); |
| 84 |
bar.free(); |
| 85 |
|
| 86 |
assert.strictEqual(wasm.ConstructorsBar.other_name, undefined); |
| 87 |
const bar2 = new wasm.ConstructorsBar(5, 6); |
| 88 |
assert.strictEqual(bar2.get_sum(), 11); |
| 89 |
bar2.free(); |
| 90 |
|
| 91 |
assert.strictEqual(wasm.cross_item_construction().get_sum(), 15); |
| 92 |
}; |
| 93 |
|
| 94 |
exports.js_empty_structs = () => { |
| 95 |
wasm.OtherEmpty.return_a_value(); |
| 96 |
}; |
| 97 |
|
| 98 |
exports.js_public_fields = () => { |
| 99 |
const a = wasm.PublicFields.new(); |
| 100 |
assert.strictEqual(a.a, 0); |
| 101 |
a.a = 3; |
| 102 |
assert.strictEqual(a.a, 3); |
| 103 |
|
| 104 |
assert.strictEqual(a.b, 0); |
| 105 |
a.b = 7; |
| 106 |
assert.strictEqual(a.b, 7); |
| 107 |
|
| 108 |
assert.strictEqual(a.c, 0); |
| 109 |
a.c = 8; |
| 110 |
assert.strictEqual(a.c, 8); |
| 111 |
|
| 112 |
assert.strictEqual(a.d, 0); |
| 113 |
a.d = 3.3; |
| 114 |
assert.strictEqual(a.d, 3); |
| 115 |
|
| 116 |
assert.strictEqual(a.skipped, undefined); |
| 117 |
}; |
| 118 |
|
| 119 |
exports.js_getter_with_clone = () => { |
| 120 |
const a = wasm.GetterWithCloneStruct.new(); |
| 121 |
assert.strictEqual(a.a, ''); |
| 122 |
a.a = 'foo'; |
| 123 |
assert.strictEqual(a.a, 'foo'); |
| 124 |
|
| 125 |
const b = wasm.GetterWithCloneStructField.new(); |
| 126 |
assert.strictEqual(b.a, ''); |
| 127 |
b.a = 'foo'; |
| 128 |
assert.strictEqual(b.a, 'foo'); |
| 129 |
}; |
| 130 |
|
| 131 |
exports.js_using_self = () => { |
| 132 |
wasm.UseSelf.new().free(); |
| 133 |
}; |
| 134 |
|
| 135 |
exports.js_readonly_fields = () => { |
| 136 |
const a = wasm.Readonly.new(); |
| 137 |
assert.strictEqual(a.a, 0); |
| 138 |
a.a = 3; |
| 139 |
assert.strictEqual(a.a, 0); |
| 140 |
a.free(); |
| 141 |
}; |
| 142 |
|
| 143 |
exports.js_double_consume = () => { |
| 144 |
const r = new wasm.DoubleConsume(); |
| 145 |
assert.throws(() => r.consume(r)); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
exports.js_js_rename = () => { |
| 150 |
(new wasm.JsRename()).bar(); |
| 151 |
wasm.classes_foo(); |
| 152 |
}; |
| 153 |
|
| 154 |
exports.js_access_fields = () => { |
| 155 |
assert.ok((new wasm.AccessFieldFoo()).bar instanceof wasm.AccessFieldBar); |
| 156 |
assert.ok((new wasm.AccessField0())[0] instanceof wasm.AccessFieldBar); |
| 157 |
}; |
| 158 |
|
| 159 |
exports.js_renamed_export = () => { |
| 160 |
const x = new wasm.JsRenamedExport(); |
| 161 |
assert.ok(x.x === 3); |
| 162 |
x.foo(); |
| 163 |
x.bar(x); |
| 164 |
}; |
| 165 |
|
| 166 |
exports.js_renamed_field = () => { |
| 167 |
const x = new wasm.RenamedField(); |
| 168 |
assert.ok(x.bar === 3); |
| 169 |
|
| 170 |
x.foo(); |
| 171 |
} |
| 172 |
|
| 173 |
exports.js_conditional_bindings = () => { |
| 174 |
const x = new wasm.ConditionalBindings(); |
| 175 |
x.free(); |
| 176 |
}; |
| 177 |
|
| 178 |
exports.js_assert_none = x => { |
| 179 |
assert.strictEqual(x, undefined); |
| 180 |
}; |
| 181 |
exports.js_assert_some = x => { |
| 182 |
assert.ok(x instanceof wasm.OptionClass); |
| 183 |
}; |
| 184 |
exports.js_return_none1 = () => null; |
| 185 |
exports.js_return_none2 = () => undefined; |
| 186 |
exports.js_return_some = x => x; |
| 187 |
|
| 188 |
exports.js_test_option_classes = () => { |
| 189 |
assert.strictEqual(wasm.option_class_none(), undefined); |
| 190 |
wasm.option_class_assert_none(undefined); |
| 191 |
wasm.option_class_assert_none(null); |
| 192 |
const c = wasm.option_class_some(); |
| 193 |
assert.ok(c instanceof wasm.OptionClass); |
| 194 |
wasm.option_class_assert_some(c); |
| 195 |
}; |
| 196 |
|
| 197 |
/** |
| 198 |
* Invokes `console.log`, but logs to a string rather than stdout |
| 199 |
* @param {any} data Data to pass to `console.log` |
| 200 |
* @returns {string} Output from `console.log`, without color or trailing newlines |
| 201 |
*/ |
| 202 |
const console_log_to_string = data => { |
| 203 |
// Store the original stdout.write and create a console that logs without color |
| 204 |
const original_write = process.stdout.write; |
| 205 |
const colorless_console = new console.Console({ |
| 206 |
stdout: process.stdout, |
| 207 |
colorMode: false |
| 208 |
}); |
| 209 |
let output = ''; |
| 210 |
|
| 211 |
// Change stdout.write to append to our string, then restore the original function |
| 212 |
process.stdout.write = chunk => output += chunk.trim(); |
| 213 |
colorless_console.log(data); |
| 214 |
process.stdout.write = original_write; |
| 215 |
|
| 216 |
return output; |
| 217 |
}; |
| 218 |
|
| 219 |
exports.js_test_inspectable_classes = () => { |
| 220 |
const inspectable = wasm.Inspectable.new(); |
| 221 |
const not_inspectable = wasm.NotInspectable.new(); |
| 222 |
// Inspectable classes have a toJSON and toString implementation generated |
| 223 |
assert.deepStrictEqual(inspectable.toJSON(), { a: inspectable.a }); |
| 224 |
assert.strictEqual(inspectable.toString(), `{"a":${inspectable.a}}`); |
| 225 |
// Inspectable classes in Node.js have improved console.log formatting as well |
| 226 |
assert(console_log_to_string(inspectable).endsWith(`{ a: ${inspectable.a} }`)); |
| 227 |
// Non-inspectable classes do not have a toJSON or toString generated |
| 228 |
assert.strictEqual(not_inspectable.toJSON, undefined); |
| 229 |
assert.strictEqual(not_inspectable.toString(), '[object Object]'); |
| 230 |
// Non-inspectable classes in Node.js have no special console.log formatting |
| 231 |
assert.strictEqual(console_log_to_string(not_inspectable), `NotInspectable { __wbg_ptr: ${not_inspectable.__wbg_ptr} }`); |
| 232 |
inspectable.free(); |
| 233 |
not_inspectable.free(); |
| 234 |
}; |
| 235 |
|
| 236 |
exports.js_test_inspectable_classes_can_override_generated_methods = () => { |
| 237 |
const overridden_inspectable = wasm.OverriddenInspectable.new(); |
| 238 |
// Inspectable classes can have the generated toJSON and toString overwritten |
| 239 |
assert.strictEqual(overridden_inspectable.a, 0); |
| 240 |
assert.deepStrictEqual(overridden_inspectable.toJSON(), 'JSON was overwritten'); |
| 241 |
assert.strictEqual(overridden_inspectable.toString(), 'string was overwritten'); |
| 242 |
overridden_inspectable.free(); |
| 243 |
}; |
| 244 |
|
| 245 |
exports.js_test_class_defined_in_macro = () => { |
| 246 |
const macroClass = new wasm.InsideMacro(); |
| 247 |
assert.strictEqual(macroClass.a, 3); |
| 248 |
macroClass.a = 5; |
| 249 |
assert.strictEqual(macroClass.a, 5); |
| 250 |
}; |