-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Wasm files that import JS resources #13608
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
942188c
Add failing test
kachkaev 1c58803
Add copyright notices
kachkaev a7eac68
Add CHANGELOG
kachkaev ea9133e
Fix
kachkaev 65223df
Update e2e/native-esm/__tests__/native-esm-wasm.test.js
kachkaev 5d4a115
Remove redundant line
kachkaev 582a86b
Merge remote-tracking branch 'origin/main' into wasm-bindgen
kachkaev 7dc9940
Merge branch 'main' into wasm-bindgen
kachkaev f115872
Update CHANGELOG.md
kachkaev 98116ea
Merge branch 'main' into wasm-bindgen
SimenB 15708bc
Merge branch 'main' into wasm-bindgen
SimenB 797437b
fix faulty merge
SimenB 2de0f1f
split for debugging
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// folder source: https://github.com/rustwasm/wasm-bindgen/tree/4f865308afbe8d2463968457711ad356bae63b71/examples/hello_world | ||
// docs: https://rustwasm.github.io/docs/wasm-bindgen/examples/hello-world.html | ||
|
||
import * as wasm from './index_bg.wasm'; | ||
export * from './index_bg.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import * as wasm from './index_bg.wasm'; | ||
|
||
const lTextDecoder = | ||
typeof TextDecoder === 'undefined' | ||
? (0, module.require)('util').TextDecoder | ||
: TextDecoder; | ||
|
||
const cachedTextDecoder = new lTextDecoder('utf-8', { | ||
fatal: true, | ||
ignoreBOM: true, | ||
}); | ||
|
||
cachedTextDecoder.decode(); | ||
|
||
let cachedUint8Memory0 = new Uint8Array(); | ||
|
||
function getUint8Memory0() { | ||
if (cachedUint8Memory0.byteLength === 0) { | ||
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); | ||
} | ||
return cachedUint8Memory0; | ||
} | ||
|
||
function getStringFromWasm0(ptr, len) { | ||
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); | ||
} | ||
|
||
function logError(f, args) { | ||
try { | ||
return f.apply(this, args); | ||
} catch (e) { | ||
const error = (function () { | ||
try { | ||
return e instanceof Error | ||
? `${e.message}\n\nStack:\n${e.stack}` | ||
: e.toString(); | ||
} catch (_) { | ||
return '<failed to stringify thrown value>'; | ||
} | ||
})(); | ||
console.error( | ||
'wasm-bindgen: imported JS function that was not marked as `catch` threw an error:', | ||
error, | ||
); | ||
throw e; | ||
} | ||
} | ||
|
||
let WASM_VECTOR_LEN = 0; | ||
|
||
const lTextEncoder = | ||
typeof TextEncoder === 'undefined' | ||
? (0, module.require)('util').TextEncoder | ||
: TextEncoder; | ||
|
||
const cachedTextEncoder = new lTextEncoder('utf-8'); | ||
|
||
const encodeString = | ||
typeof cachedTextEncoder.encodeInto === 'function' | ||
? function (arg, view) { | ||
return cachedTextEncoder.encodeInto(arg, view); | ||
} | ||
: function (arg, view) { | ||
const buf = cachedTextEncoder.encode(arg); | ||
view.set(buf); | ||
return { | ||
read: arg.length, | ||
written: buf.length, | ||
}; | ||
}; | ||
|
||
function passStringToWasm0(arg, malloc, realloc) { | ||
if (typeof arg !== 'string') throw new Error('expected a string argument'); | ||
|
||
if (realloc === undefined) { | ||
const buf = cachedTextEncoder.encode(arg); | ||
const ptr = malloc(buf.length); | ||
getUint8Memory0() | ||
.subarray(ptr, ptr + buf.length) | ||
.set(buf); | ||
WASM_VECTOR_LEN = buf.length; | ||
return ptr; | ||
} | ||
|
||
let len = arg.length; | ||
let ptr = malloc(len); | ||
|
||
const mem = getUint8Memory0(); | ||
|
||
let offset = 0; | ||
|
||
for (; offset < len; offset++) { | ||
const code = arg.charCodeAt(offset); | ||
if (code > 0x7f) break; | ||
mem[ptr + offset] = code; | ||
} | ||
|
||
if (offset !== len) { | ||
if (offset !== 0) { | ||
arg = arg.slice(offset); | ||
} | ||
ptr = realloc(ptr, len, (len = offset + arg.length * 3)); | ||
const view = getUint8Memory0().subarray(ptr + offset, ptr + len); | ||
const ret = encodeString(arg, view); | ||
if (ret.read !== arg.length) throw new Error('failed to pass whole string'); | ||
offset += ret.written; | ||
} | ||
|
||
WASM_VECTOR_LEN = offset; | ||
return ptr; | ||
} | ||
/** | ||
* @param {string} name | ||
*/ | ||
export function greet(name) { | ||
const ptr0 = passStringToWasm0( | ||
name, | ||
wasm.__wbindgen_malloc, | ||
wasm.__wbindgen_realloc, | ||
); | ||
const len0 = WASM_VECTOR_LEN; | ||
wasm.greet(ptr0, len0); | ||
} | ||
|
||
export function __wbg_alert_9ea5a791b0d4c7a3() { | ||
return logError((arg0, arg1) => { | ||
// eslint-disable-next-line no-undef | ||
alert(getStringFromWasm0(arg0, arg1)); | ||
}, arguments); | ||
} | ||
|
||
export function __wbindgen_throw(arg0, arg1) { | ||
throw new Error(getStringFromWasm0(arg0, arg1)); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks so, yes. All three files in this folder are auto-generated and I’ve decided to keep them as is. This will be helpful in the future if we need to re-generate the example again.
Maybe this like has its meaning actually. It makes sure that the wasm file is imported before JS, which may affect the result.
index_bg.wasm
andindex_bg.js
import stuff from each other,bg
stands forbindgen
.