Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Aug 26, 2023
1 parent 2a17578 commit b58a386
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ root = true
charset = utf-8
trim_trailing_whitespace = true
end_of_line = lf
indent_style = space
indent_style = tab
insert_final_newline = true
indent_size = 4
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
# Go workspace file
go.work

lib.wasm
gofmt.wasm
*.tgz
go_wasm.js
gofmt.js
18 changes: 9 additions & 9 deletions gen_patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ tmp_dir=$(mktemp -d)
cd $tmp_dir
git init

cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js $tmp_dir/go_wasm.js
cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js $tmp_dir/gofmt.js
git add -f .
git commit -m "init"

cp $current_dir/go_wasm.js $tmp_dir/go_wasm.js
cp $current_dir/gofmt.js $tmp_dir/gofmt.js
git add -f .

git diff \
--cached \
--no-color \
--ignore-space-at-eol \
--no-ext-diff \
--src-prefix=a/ \
--dst-prefix=b/ \
>$current_dir/go_wasm.patch
--cached \
--no-color \
--ignore-space-at-eol \
--no-ext-diff \
--src-prefix=a/ \
--dst-prefix=b/ \
>$current_dir/gofmt.patch

rm -rf $tmp_dir
10 changes: 5 additions & 5 deletions lib.d.ts → gofmt.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export type InitInput =
| RequestInfo
| URL
| Response
| BufferSource
| WebAssembly.Module;
| RequestInfo
| URL
| Response
| BufferSource
| WebAssembly.Module;

export default function init(wasm_url?: InitInput): Promise<void>;
export declare function format(input: string): string;
94 changes: 82 additions & 12 deletions go_wasm.patch → gofmt.patch
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
diff --git a/go_wasm.js b/go_wasm.js
index 8021b44..a4836ba 100644
--- a/go_wasm.js
+++ b/go_wasm.js
@@ -4,135 +4,10 @@
diff --git a/gofmt.js b/gofmt.js
index 8021b44..f7bfc3f 100644
--- a/gofmt.js
+++ b/gofmt.js
@@ -3,136 +3,10 @@
// license that can be found in the LICENSE file.
//
// This file has been modified for use by the TinyGo compiler.
-
-(() => {
- // Map multiple JavaScript environments to a single common API,
- // preferring web standards over Node.js API.
Expand Down Expand Up @@ -135,11 +136,11 @@ index 8021b44..a4836ba 100644
- var logLine = [];

- global.Go = class {
+ export class Go {
+ class Go {
constructor() {
this._callbackTimeouts = new Map();
this._nextCallbackTimeoutID = 1;
@@ -249,50 +124,12 @@
@@ -249,50 +123,12 @@
this.importObject = {
wasi_snapshot_preview1: {
// https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_write
Expand Down Expand Up @@ -196,7 +197,7 @@ index 8021b44..a4836ba 100644
},
env: {
// func ticks() float64
@@ -307,10 +144,15 @@
@@ -307,10 +143,15 @@
},

// func finalizeRef(v ref)
Expand All @@ -216,7 +217,7 @@ index 8021b44..a4836ba 100644
},

// func stringVal(value string) ref
@@ -464,7 +306,12 @@
@@ -464,7 +305,12 @@
null,
true,
false,
Expand All @@ -230,7 +231,7 @@ index 8021b44..a4836ba 100644
this,
];
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
@@ -472,8 +319,6 @@
@@ -472,8 +318,6 @@
this._idPool = []; // unused ids that have been garbage collected
this.exited = false; // whether the Go program has exited

Expand All @@ -239,7 +240,7 @@ index 8021b44..a4836ba 100644
while (true) {
const callbackPromise = new Promise((resolve) => {
this._resolveCallbackPromise = () => {
@@ -511,25 +356,3 @@
@@ -511,25 +355,72 @@
};
}
}
Expand All @@ -265,3 +266,72 @@ index 8021b44..a4836ba 100644
- });
- }
-})();
+/**
+ * ================== End of wasm_exec.js ==================
+ */
+/**/let wasm;
+/**/export default async function init(input) {
+/**/ if (wasm) {
+/**/ await wasm;
+/**/ return;
+/**/ }
+/**/ const go = new Go();
+/**/ wasm = load(input, go.importObject);
+/**/ wasm = await wasm;
+/**/ go.run(wasm.instance);
+/**/}
+/**/
+/**/async function load(module, importObject) {
+/**/ switch (typeof module) {
+/**/ case "undefined":
+/**/ module = "gofmt.wasm";
+/**/ case "string":
+/**/ module = new URL(module, import.meta.url);
+/**/ }
+/**/ if (module instanceof URL || module instanceof Request) {
+/**/ if (
+/**/ typeof __webpack_require__ !== "function" &&
+/**/ module.protocol === "file:"
+/**/ ) {
+/**/ const fs = await import("node:fs/promises");
+/**/ module = await fs.readFile(module);
+/**/ } else {
+/**/ module = await fetch(module);
+/**/ }
+/**/ }
+/**/ if (module instanceof Response) {
+/**/ if ("instantiateStreaming" in WebAssembly) {
+/**/ try {
+/**/ return await WebAssembly.instantiateStreaming(
+/**/ module,
+/**/ importObject
+/**/ );
+/**/ } 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, importObject);
+/**/ }
+/**/ const instance = await WebAssembly.instantiate(module, importObject);
+/**/ if (instance instanceof WebAssembly.Instance) {
+/**/ return { instance, module };
+/**/ }
+/**/ return instance;
+/**/}
+/**/
+/**/export function format(input) {
+/**/ const [err, result] = wasm.instance.format(input);
+/**/ if (err) {
+/**/ throw new Error(result);
+/**/ }
+/**/ return result;
+/**/}
+/**/
75 changes: 0 additions & 75 deletions lib.js

This file was deleted.

96 changes: 48 additions & 48 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
{
"name": "@wasm-fmt/gofmt",
"description": "A wasm based golang formatter",
"author": "magic-akari <[email protected]>",
"version": "0.4.0",
"license": "MIT",
"keywords": [
"wasm",
"golang",
"formatter"
],
"repository": {
"type": "git",
"url": "https://github.com/wasm-fmt/gofmt"
},
"homepage": "https://github.com/wasm-fmt/gofmt",
"bugs": {
"url": "https://github.com/wasm-fmt/gofmt/issues"
},
"type": "module",
"main": "lib.js",
"module": "lib.js",
"types": "lib.d.ts",
"exports": {
".": {
"types": "./lib.d.ts",
"default": "./lib.js"
},
"./vite": {
"types": "./lib.d.ts",
"default": "./vite.js"
},
"./package.json": "./package.json",
"./*": "./*"
},
"scripts": {
"go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
"patch": "git apply ./go_wasm.patch",
"build": "tinygo build -o=lib.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
"postbuild": "npm run go_wasm && npm run patch",
"test": "node --test"
},
"engines": {
"node": ">=16.17.0"
},
"publishConfig": {
"access": "public",
"provenance": true
}
"name": "@wasm-fmt/gofmt",
"description": "A wasm based golang formatter",
"author": "magic-akari <[email protected]>",
"version": "0.4.0",
"license": "MIT",
"keywords": [
"wasm",
"golang",
"formatter"
],
"repository": {
"type": "git",
"url": "https://github.com/wasm-fmt/gofmt"
},
"homepage": "https://github.com/wasm-fmt/gofmt",
"bugs": {
"url": "https://github.com/wasm-fmt/gofmt/issues"
},
"type": "module",
"main": "gofmt.js",
"module": "gofmt.js",
"types": "gofmt.d.ts",
"exports": {
".": {
"types": "./gofmt.d.ts",
"default": "./gofmt.js"
},
"./vite": {
"types": "./gofmt.d.ts",
"default": "./vite.js"
},
"./package.json": "./package.json",
"./*": "./*"
},
"scripts": {
"gofmt": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./gofmt.js",
"patch": "git apply ./gofmt.patch",
"build": "tinygo build -o=gofmt.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
"postbuild": "npm run gofmt && npm run patch",
"test": "node --test"
},
"engines": {
"node": ">=16.17.0"
},
"publishConfig": {
"access": "public",
"provenance": true
}
}
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import test from "node:test";
import init, { format } from "./lib.js";
import init, { format } from "./gofmt.js";

await init();

Expand Down
Loading

0 comments on commit b58a386

Please sign in to comment.