wasm-bindgen/tests/wasm/imports.js

130 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-08-05 09:52:11 -07:00
const assert = require('assert');
const wasm = require('wasm-bindgen-test');
const fs = require('fs');
2018-08-05 09:52:11 -07:00
let ARG = null;
let ANOTHER_ARG = null;
let SYM = Symbol('a');
exports.simple_foo = function(s) {
assert.strictEqual(ARG, null);
assert.strictEqual(s, "foo");
ARG = s;
};
exports.simple_another = function(s) {
assert.strictEqual(ANOTHER_ARG, null);
assert.strictEqual(s, 21);
ANOTHER_ARG = s;
return 35;
};
exports.simple_take_and_return_bool = function(s) {
return s;
};
exports.simple_return_object = function() {
return SYM;
};
exports.test_simple = function() {
assert.strictEqual(ARG, null);
wasm.simple_take_str("foo");
assert.strictEqual(ARG, "foo");
assert.strictEqual(ANOTHER_ARG, null);
assert.strictEqual(wasm.simple_another_thunk(21), 35);
assert.strictEqual(ANOTHER_ARG, 21);
assert.strictEqual(wasm.simple_bool_thunk(true), true);
assert.strictEqual(wasm.simple_bool_thunk(false), false);
assert.strictEqual(wasm.simple_get_the_object(), SYM);
};
exports.return_string = function() {
return 'bar';
};
exports.take_and_ret_string = function(a) {
return a + 'b';
};
exports.exceptions_throw = function() {
throw new Error('error!');
};
exports.exceptions_throw2 = function() {
throw new Error('error2');
};
exports.test_exception_propagates = function() {
assert.throws(wasm.exceptions_propagate, /error!/);
};
exports.assert_valid_error = function(obj) {
assert.strictEqual(obj instanceof Error, true);
assert.strictEqual(obj.message, 'error2');
};
exports.IMPORT = 1.0;
exports.return_three = function() { return 3; };
exports.underscore = function(x) {};
exports.pub = function() { return 2; };
2018-08-05 09:52:11 -07:00
exports.bar = { foo: 3 };
let CUSTOM_TYPE = null;
exports.take_custom_type = function(f) {
CUSTOM_TYPE = f;
return f;
};
exports.custom_type_return_2 = function() {
return 2;
};
exports.touch_custom_type = function() {
assert.throws(() => CUSTOM_TYPE.touch(),
/Attempt to use a moved value|null pointer passed to rust/);
2018-08-05 09:52:11 -07:00
};
exports.interpret_2_as_custom_type = function() {
Second large refactor for WebIDL bindings This commit is the second, and hopefully last massive, refactor for using WebIDL bindings internally in `wasm-bindgen`. This commit actually fully executes on the task at hand, moving `wasm-bindgen` to internally using WebIDL bindings throughout its code generation, anyref passes, etc. This actually fixes a number of issues that have existed in the anyref pass for some time now! The main changes here are to basically remove the usage of `Descriptor` from generating JS bindings. Instead two new types are introduced: `NonstandardIncoming` and `NonstandardOutgoing` which are bindings lists used for incoming/outgoing bindings. These mirror the standard terminology and literally have variants which are the standard values. All `Descriptor` types are now mapped into lists of incoming/outgoing bindings and used for process in wasm-bindgen. All JS generation has been refactored and updated to now process these lists of bindings instead of the previous `Descriptor`. In other words this commit takes `js2rust.rs` and `rust2js.rs` and first splits them in two. Interpretation of `Descriptor` and what to do for conversions is in the binding selection modules. The actual generation of JS from the binding selection is now performed by `incoming.rs` and `outgoing.rs`. To boot this also deduplicates all the code between the argument handling of `js2rust.rs` and return value handling of `rust2js.rs`. This means that to implement a new binding you only need to implement it one place and it's implemented for free in the other! This commit is not the end of the story though. I would like to add a mdoe to `wasm-bindgen` that literally emits a WebIDL bindings section. That's left for a third (and hopefully final) refactoring which is also intended to optimize generated JS for bindings. This commit currently loses the optimization where an imported is hooked up by value directly whenever a shim isn't needed. It's planned that the next refactoring to emit a webidl binding section that can be added back in. It shouldn't be too too hard hopefully since all the scaffolding is in place now. cc #1524
2019-06-10 07:09:51 -07:00
assert.throws(wasm.interpret_2_as_custom_type, /expected instance of CustomType/);
2018-08-05 09:52:11 -07:00
};
2018-08-06 09:51:12 -07:00
exports.baz$ = function() {};
exports.$foo = 1.0;
exports.assert_dead_import_not_generated = function() {
const filename = require.resolve("wasm-bindgen-test");
const bindings = fs.readFileSync(filename);
assert.ok(!bindings.includes("unused_import"));
};
exports.import_inside_function_works = function() {};
exports.import_inside_private_module = function() {};
exports.should_call_undefined_functions = () => false;
exports.STATIC_STRING = 'x';
class StaticMethodCheck {
static static_method_of_right_this() {
assert.ok(this === StaticMethodCheck);
}
}
exports.StaticMethodCheck = StaticMethodCheck;
Rewrite wasm-bindgen with updated interface types proposal (#1882) This commit is a pretty large scale rewrite of the internals of wasm-bindgen. No user-facing changes are expected as a result of this PR, but due to the scale of changes here it's likely inevitable that at least something will break. I'm hoping to get more testing in though before landing! The purpose of this PR is to update wasm-bindgen to the current state of the interface types proposal. The wasm-bindgen tool was last updated when it was still called "WebIDL bindings" so it's been awhile! All support is now based on https://github.com/bytecodealliance/wasm-interface-types which defines parsers/binary format/writers/etc for wasm-interface types. This is a pretty massive PR and unfortunately can't really be split up any more afaik. I don't really expect realistic review of all the code here (or commits), but some high-level changes are: * Interface types now consists of a set of "adapter functions". The IR in wasm-bindgen is modeled the same way not. * Each adapter function has a list of instructions, and these instructions work at a higher level than wasm itself, for example with strings. * The wasm-bindgen tool has a suite of instructions which are specific to it and not present in the standard. (like before with webidl bindings) * The anyref/multi-value transformations are now greatly simplified. They're simply "optimization passes" over adapter functions, removing instructions that are otherwise present. This way we don't have to juggle so much all over the place, and instructions always have the same meaning.
2019-12-03 11:16:44 -06:00
exports.receive_undefined = val => {
assert.strictEqual(val, undefined);
};
const VAL = {};
exports.receive_some = val => {
assert.strictEqual(val, VAL);
};
exports.get_some_val = () => VAL;