Skip to content

Commit

Permalink
feat: add wasm runtime package (#1904)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jan 16, 2024
1 parent ddeaf30 commit 120accd
Show file tree
Hide file tree
Showing 24 changed files with 4,437 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build_and_test: &BUILD_AND_TEST
yarn --version
test_script:
- yarn install --immutable --mode=skip-build
- yarn build
- yarn lerna run build --scope '@napi-rs/*' --ignore '@napi-rs/wasm-runtime'
- cargo test -p napi-sys --lib -- --nocapture
- yarn build:test
- yarn test --verbose
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ overrides:
- '@typescript-eslint'
parserOptions:
project:
- ./examples/tsconfig.json
- ./tsconfig.json
rules:
'import/order':
[
Expand Down
49 changes: 49 additions & 0 deletions .yarn/patches/buffer-npm-6.0.3-cd90dfedfe.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
diff --git a/index.js b/index.js
index 7a0e9c2a123bc9d26c20bb3de4a3c4e49b24ee40..b1a53729c2900ddfca88a0f312a785c51a5aac80 100644
--- a/index.js
+++ b/index.js
@@ -8,19 +8,17 @@

'use strict'

-const base64 = require('base64-js')
-const ieee754 = require('ieee754')
+import * as base64 from 'base64-js'
+import * as ieee754 from 'ieee754'
const customInspectSymbol =
(typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
: null

-exports.Buffer = Buffer
-exports.SlowBuffer = SlowBuffer
-exports.INSPECT_MAX_BYTES = 50
+const INSPECT_MAX_BYTES = 50

const K_MAX_LENGTH = 0x7fffffff
-exports.kMaxLength = K_MAX_LENGTH
+const kMaxLength = K_MAX_LENGTH

/**
* If `Buffer.TYPED_ARRAY_SUPPORT`:
@@ -616,7 +614,7 @@ Buffer.prototype.equals = function equals (b) {

Buffer.prototype.inspect = function inspect () {
let str = ''
- const max = exports.INSPECT_MAX_BYTES
+ const max = INSPECT_MAX_BYTES
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
if (this.length > max) str += ' ... '
return '<Buffer ' + str + '>'
@@ -2104,3 +2102,10 @@ function defineBigIntMethod (fn) {
function BufferBigIntNotDefined () {
throw new Error('BigInt not supported')
}
+
+export {
+ Buffer,
+ SlowBuffer,
+ INSPECT_MAX_BYTES,
+ kMaxLength,
+}
\ No newline at end of file
3,839 changes: 3,839 additions & 0 deletions .yarn/patches/node-inspect-extracted-npm-3.0.0-f661b6c334.patch

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions cli/src/api/templates/load-wasi-template.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export const createWasiBrowserBinding = (
wasiFilename: string,
wasiRegisterFunctions: string[],
) => `import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core'
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
import { WASI as __WASI } from '@tybys/wasm-util'
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser'
) => `import {
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
getDefaultContext as __emnapiGetDefaultContext,
WASI as __WASI,
} from '@napi-rs/wasm-runtime'
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
import __wasmUrl from './${wasiFilename}.wasm?url'
Expand Down
5 changes: 2 additions & 3 deletions cli/src/api/templates/wasi-worker-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ globalThis.onmessage = function (e) {
};
`

export const WASI_WORKER_BROWSER_TEMPLATE = `import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
import { WASI } from '@tybys/wasm-util'
import { Volume, createFsFromVolume } from 'memfs-browser'
export const WASI_WORKER_BROWSER_TEMPLATE = `import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
import { Volume, createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
const fs = createFsFromVolume(
Volume.fromJSON({
Expand Down
2 changes: 1 addition & 1 deletion examples/napi/__tests__/__snapshots__/values.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Generated by [AVA](https://avajs.dev).
[
'@napi-rs/cli',
'@napi-rs/wasm-runtime',
'@types/lodash',
'@vitest/browser',
'@vitest/ui',
'ava',
'cross-env',
'electron',
'lodash',
'memfs-browser',
'sinon',
'vite',
'vite-plugin-node-polyfills',
Expand Down
Binary file modified examples/napi/__tests__/__snapshots__/values.spec.ts.snap
Binary file not shown.
9 changes: 2 additions & 7 deletions examples/napi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="https://napi.rs/img/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NAPI-RS examples playground</title>
<title>Vite Playground</title>
<script type="module" src="./vite-entry.js"></script>
</head>

<body>
<div id="app" />
</body>
</html>
10 changes: 6 additions & 4 deletions examples/napi/index.wasi-browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core'
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
import { WASI as __WASI } from '@tybys/wasm-util'
import {
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
getDefaultContext as __emnapiGetDefaultContext,
WASI as __WASI,
} from '@napi-rs/wasm-runtime'
import {
Volume as __Volume,
createFsFromVolume as __createFsFromVolume,
} from 'memfs-browser'
} from '@napi-rs/wasm-runtime/fs'

import __wasmUrl from './index.wasm?url'

Expand Down
Loading

0 comments on commit 120accd

Please sign in to comment.