Skip to content

Commit

Permalink
update go.js
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed May 2, 2024
1 parent 85128fc commit fe23fb4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
13 changes: 12 additions & 1 deletion npm-packages/cadence-language-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ so it can be used in tools written in JavaScript.

## Releasing

To release a new version of the Language server NPM package all you need to do is create a release of Langauge server and GitHub action will also publish a new version of WebAssembly built binary to NPM.
To release a new version of the Language server NPM package all you need to do is create a release of Langauge server and GitHub action will also publish a new version of WebAssembly built binary to NPM.
That newly build NPM package using the WebAssembly will be published and can be found on NPM https://www.npmjs.com/package/@onflow/cadence-language-server

## Development

### Updating `src/go.js`

- Copy `misc/wasm/wasm_exec.js` of appropriate Go version into `src/go.js`.
Keep the last line (`export const go = new Go();`)
- Update the version in the header
- Remove the anonymous function wrapper
- Change `globalThis.Go = class {` to `class Go {`
- Run `npx prettier -w src/go.js`
22 changes: 22 additions & 0 deletions npm-packages/cadence-language-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion npm-packages/cadence-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"node-fetch": "^2.6.1",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2",
"vscode-languageserver-protocol": "^3.17.5"
"vscode-languageserver-protocol": "^3.17.5",
"prettier": "^3.2.5"
},
"files": [
"dist/**/*"
Expand Down
33 changes: 21 additions & 12 deletions npm-packages/cadence-language-server/src/go.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Adapted from https://github.com/golang/go/blob/go1.20.14/misc/wasm/wasm_exec.js
// Adapted from https://github.com/golang/go/blob/go1.21.9/misc/wasm/wasm_exec.js

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

"use strict";

const enosys = () => {
const err = new Error("not implemented");
err.code = "ENOSYS";
Expand Down Expand Up @@ -143,13 +145,13 @@ if (!globalThis.process) {

if (!globalThis.crypto) {
throw new Error(
"globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"
"globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)",
);
}

if (!globalThis.performance) {
throw new Error(
"globalThis.performance is not available, polyfill required (performance.now only)"
"globalThis.performance is not available, polyfill required (performance.now only)",
);
}

Expand Down Expand Up @@ -185,6 +187,10 @@ class Go {
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
};

const setInt32 = (addr, v) => {
this.mem.setUint32(addr + 0, v, true);
};

const getInt64 = (addr) => {
const low = this.mem.getUint32(addr + 0, true);
const high = this.mem.getInt32(addr + 4, true);
Expand Down Expand Up @@ -274,13 +280,16 @@ class Go {
const saddr = getInt64(addr + 0);
const len = getInt64(addr + 8);
return decoder.decode(
new DataView(this._inst.exports.mem.buffer, saddr, len)
new DataView(this._inst.exports.mem.buffer, saddr, len),
);
};

const timeOrigin = Date.now() - performance.now();
this.importObject = {
go: {
_gotest: {
add: (a, b) => a + b,
},
gojs: {
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
Expand Down Expand Up @@ -345,8 +354,8 @@ class Go {
this._resume();
}
},
getInt64(sp + 8) + 1 // setTimeout has been seen to fire up to 1 millisecond early
)
getInt64(sp + 8),
),
);
this.mem.setInt32(sp + 16, id, true);
},
Expand Down Expand Up @@ -398,7 +407,7 @@ class Go {
Reflect.set(
loadValue(sp + 8),
loadString(sp + 16),
loadValue(sp + 32)
loadValue(sp + 32),
);
},

Expand All @@ -413,7 +422,7 @@ class Go {
sp >>>= 0;
storeValue(
sp + 24,
Reflect.get(loadValue(sp + 8), getInt64(sp + 16))
Reflect.get(loadValue(sp + 8), getInt64(sp + 16)),
);
},

Expand Down Expand Up @@ -501,7 +510,7 @@ class Go {
sp >>>= 0;
this.mem.setUint8(
sp + 24,
loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0
loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0,
);
},

Expand Down Expand Up @@ -615,7 +624,7 @@ class Go {
const wasmMinDataAddr = 4096 + 8192;
if (offset >= wasmMinDataAddr) {
throw new Error(
"total length of command line and environment variables exceeds limit"
"total length of command line and environment variables exceeds limit",
);
}

Expand Down Expand Up @@ -645,6 +654,6 @@ class Go {
return event.result;
};
}
}
};

export const go = new Go();

0 comments on commit fe23fb4

Please sign in to comment.