Skip to content

Commit

Permalink
add wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
1595901624 committed Sep 8, 2023
1 parent 4669d1f commit 2e08414
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 145 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import init, {parse_json_default} from './wasm/rust_json_lib_rs';
import * as wasm_module from './wasm/rust_json_lib_rs';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
Expand Down Expand Up @@ -49,7 +49,9 @@ export function activate(context: vscode.ExtensionContext) {
"age": 1
}
//webviewPanel.webview.html = '<h1>RustJson</h1>';
webviewPanel.webview.html = '<h1>' + parse_json_default(JSON.stringify(json)) +'</h1>';
// 加载本地html
// webviewPanel.webview.html = fs.readFileSync(path.join(context.extensionPath, 'src', 'customView', 'customView.html'), 'utf-8');
webviewPanel.webview.html = '<h1>' + wasm_module.parse_json_default(JSON.stringify(json)) +'</h1>';

const quickPick = vscode.window.createQuickPick();
quickPick.items = [
Expand Down
33 changes: 0 additions & 33 deletions src/wasm/rust_json_lib_rs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,3 @@ export function sum(a: number, b: number): number;
* @returns {string}
*/
export function parse_json_default(json: string): string;

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly sum: (a: number, b: number) => number;
readonly parse_json_default: (a: number, b: number, c: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
}

export type SyncInitInput = BufferSource | WebAssembly.Module;
/**
* Instantiates the given `module`, which can either be bytes or
* a precompiled `WebAssembly.Module`.
*
* @param {SyncInitInput} module
*
* @returns {InitOutput}
*/
export function initSync(module: SyncInitInput): InitOutput;

/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
109 changes: 15 additions & 94 deletions src/wasm/rust_json_lib_rs.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
let imports = {};
let wasm;
const { TextEncoder, TextDecoder } = require(`util`);

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
export function sum(a, b) {
module.exports.sum = function(a, b) {
const ret = wasm.sum(a, b);
return ret;
}
};

let WASM_VECTOR_LEN = 0;

Expand All @@ -21,7 +23,7 @@ function getUint8Memory0() {
return cachedUint8Memory0;
}

const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } );
let cachedTextEncoder = new TextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
Expand Down Expand Up @@ -83,9 +85,9 @@ function getInt32Memory0() {
return cachedInt32Memory0;
}

const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
cachedTextDecoder.decode();

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
Expand All @@ -95,7 +97,7 @@ function getStringFromWasm0(ptr, len) {
* @param {string} json
* @returns {string}
*/
export function parse_json_default(json) {
module.exports.parse_json_default = function(json) {
let deferred2_0;
let deferred2_1;
try {
Expand All @@ -112,94 +114,13 @@ export function parse_json_default(json) {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
}
}

async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);

} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);

} else {
throw e;
}
}
}

const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);

} else {
const instance = await WebAssembly.instantiate(module, imports);

if (instance instanceof WebAssembly.Instance) {
return { instance, module };

} else {
return instance;
}
}
}

function __wbg_get_imports() {
const imports = {};
imports.wbg = {};

return imports;
}

function __wbg_init_memory(imports, maybe_memory) {

}

function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;
cachedInt32Memory0 = null;
cachedUint8Memory0 = null;


return wasm;
}

function initSync(module) {
if (wasm !== undefined) return wasm;

const imports = __wbg_get_imports();

__wbg_init_memory(imports);

if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}
};

const instance = new WebAssembly.Instance(module, imports);
const path = require('path').join(__dirname, 'rust_json_lib_rs_bg.wasm');
const bytes = require('fs').readFileSync(path);

return __wbg_finalize_init(instance, module);
}

async function __wbg_init(input) {
if (wasm !== undefined) return wasm;

if (typeof input === 'undefined') {
input = new URL('rust_json_lib_rs_bg.wasm', import.meta.url);
}
const imports = __wbg_get_imports();

if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
}

__wbg_init_memory(imports);

const { instance, module } = await __wbg_load(await input, imports);

return __wbg_finalize_init(instance, module);
}
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;

export { initSync }
export default __wbg_init;
Binary file modified src/wasm/rust_json_lib_rs_bg.wasm
Binary file not shown.
42 changes: 26 additions & 16 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
'use strict';

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const HtmlWebpackPlugin = require('html-webpack-plugin');
// 赋予 webpack 处理 wasm 才能的插件
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
// const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
const webpack = require('webpack');

//@ts-check
Expand Down Expand Up @@ -35,7 +35,7 @@ const extensionConfig = {
rules: [
{
test: /\.ts$/,
exclude: [/node_modules/, path.resolve('src/wasm/rust_json_lib_rs_bg.wasm')],
exclude: [/node_modules/],
use: [
{
loader: 'ts-loader'
Expand All @@ -46,24 +46,34 @@ const extensionConfig = {
// test: /\.wasm$/,
// use: [
// {
// loader: 'wasm-loader'
// loader: 'file-loader'
// }
// ]
// }
// 输出目录下的wasm文件到根目录
{
test: /\.wasm$/,
type: "javascript/auto",
loader: "wasm-loader",
options: {
name: "[name].[ext]",
outputPath: "./",
},
},
]
},
plugins: [
new HtmlWebpackPlugin(),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, ".")
}),
// Have this example work in Edge which doesn't ship `TextEncoder` or
// `TextDecoder` at this time. 处理浏览器兼容问题
// new webpack.ProvidePlugin({
// TextDecoder: ['text-encoding', 'TextDecoder'],
// TextEncoder: ['text-encoding', 'TextEncoder']
// })
],
// plugins: [
// new HtmlWebpackPlugin(),
// new WasmPackPlugin({
// crateDirectory: path.resolve(__dirname, ".")
// }),
// // Have this example work in Edge which doesn't ship `TextEncoder` or
// // `TextDecoder` at this time. 处理浏览器兼容问题
// // new webpack.ProvidePlugin({
// // TextDecoder: ['text-encoding', 'TextDecoder'],
// // TextEncoder: ['text-encoding', 'TextEncoder']
// // })
// ],
devtool: 'nosources-source-map',
infrastructureLogging: {
level: "log", // enables logging required for problem matchers
Expand Down

0 comments on commit 2e08414

Please sign in to comment.