Skip to content
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

[AssemblyScript] porting source from JS to AS #421

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from

Conversation

StEvUgnIn
Copy link

Follows #420

@StEvUgnIn
Copy link
Author

StEvUgnIn commented Mar 27, 2021

Final steps are:

  • Fix typings between JS and AS/TS
  • Be able to work fully with emitted JS from AS implementation
  • Write an encapsulated interface layer for using WASM from any web programming environment
  • Port Mocha to AS/TS spec (as-pect)
  • Benchmark performances between JS and AS modes

Please comment here if you are interested to generously help. ⚡✨🚀

--
Documentation
https://mochajs.org/
https://tenner-joshua.gitbook.io/as-pect/

@trusktr
Copy link

trusktr commented Mar 28, 2021

This will be neat for AS users to have!

Is there a particular reason to commit the build artefacts to the repo? Those can be built during publish time when publishing to NPM for example.

Why build Wasm modules?

If people will import the JS code into their JS projects, or import the AS code into their AS projects, then the module builds would not be needed. The Wasm modules are needed only for JS users who would import the Wasm modules instead of the JS code.

Wondering if that's it's worth building modules. Would the barrier between JS and Wasm allow this to be faster?

It would be sweet to see what perf diff looks like in a JS example that makes many calls into the module, vs plain JS and vs plain AS.

@StEvUgnIn
Copy link
Author

Hi @trusktr

This will be neat for AS users to have!

Thanks! I hope we could see more WebGL frameworks emerge on the WebAssembly platform and this helps to train programming in Assemblyscript 🙂

Is there a particular reason to commit the build artefacts to the repo? Those can be built during publish time when publishing to NPM for example.

I was unsure at first, but I decided to include this because of the AssemblyScript conventions and I suppose someone could just download the file to include it in an existing project, especially during this testing phase.

Why build Wasm modules?

If people will import the JS code into their JS projects, or import the AS code into their AS projects, then the module builds would not be needed. The Wasm modules are needed only for JS users who would import the Wasm modules instead of the JS code.

Yes, exactly.

A default .gitignore lies in the build repository which actually lets you keep the built modules by default.

Wondering if that's it's worth building modules. Would the barrier between JS and Wasm allow this to be faster?

It would be sweet to see what perf diff looks like in a JS example that makes many calls into the module, vs plain JS and vs plain AS.

I am looking forward to try both implementations from the browser 😃

asconfig.json Outdated Show resolved Hide resolved
- disable size optimization
- disable assertions in production

Co-authored-by: Max Graey <[email protected]>
@StEvUgnIn
Copy link
Author

StEvUgnIn commented Mar 30, 2021

I have a weird compiler output due to type declared inside modules yet..
https://gist.github.com/StEvUgnIn/acbfed089a24b1c671f79b1a36723a06

But so far, it seems to go well.

Edit:
I have modified the utility bundle-dts.ts, so it now looks for these type problems until I find how to declare types else where using typescript

diff --git a/utils/bundle-dts.js b/utils/bundle-dts.js
index c7bcd55..82be43a 100644
--- a/utils/bundle-dts.js
+++ b/utils/bundle-dts.js
@@ -2,7 +2,7 @@ const fs = require("fs");
 const path = require("path");

 let sourcePath = "./dist/index.d.ts";
-let sourceTypingsPath = "./src/types.d.ts";
+let sourceTypingsPath = "./assembly/index.d.ts";
 let sourceTypings = fs.readFileSync(sourceTypingsPath, "utf-8");
 let typings = fs.readFileSync(sourcePath, "utf-8");
 let typingsLength = typings.length;
@@ -37,4 +37,12 @@ typings = "\n" + sourceTypings.replace(/declare/g, "export") + "\n" + typings;
 // Wrap them in a "gl-matrix module"
 typings = 'declare module "gl-matrix" {\n' + typings + "\n}";

+// Place assemblyscript reference path to the top
+typings = typings.replace(/\n\n\/\/\/ <reference path=\"[^\"]+?\" \/>/g, "");
+
+typings = `
+/// <reference path="../node_modules/assemblyscript/std/portable/index.d.ts" />\n
+${typings}
+`;
+
 fs.writeFileSync(sourcePath, typings, "utf-8");

@StEvUgnIn
Copy link
Author

image
JavaScript output now renders

Copy link
Author

@StEvUgnIn StEvUgnIn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This work finalizes the first build targeting Javascript and Webassembly.

must declare hypot3, hypot4, hypot7, ... each for function overloading
moved namespace Math into maths.ts
Copy link
Author

@StEvUgnIn StEvUgnIn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msedge_BdF7kMQpT4

rollup supports operation for both release and debug
script allows to produce the exports for each loader files
loader is compiled from JS using typescript but babel is supported
loader/index.js Outdated

let modules;

wasm({ ...imports }).then(instance => {
Copy link
Author

@StEvUgnIn StEvUgnIn Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary, before I implement a better solution for exporting WebAssembly modules. Solutions are either:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently experimenting with as-bind

global function min cannot be accessed inside class when overriden
asconfig.json: tdsFiles, exportRuntime, runtime set to incremental
moved loader release and debug to assembly/loader
use as-bind for importing AS wasm
loader produces declaration files only
prepare build system for as-pect files
default configuration
copied and renamed mocha tests from *.js into *.spec.ts
@mrchantey
Copy link

mrchantey commented Feb 21, 2022

32 Bit Support

This PR is great! I'm in the process of implementing the brach in my project now.
I'd just like to flag the 64 bit operations. We may want to have flexibility, for either allowing users to make the call at runtime, or exporting two seperate builds.
For starters we could declare something like the following in common.ts and then use it throughout the library (I have no idea if this actually compiles)

//64 
export type Float = f64
export type FloatArray = Float64Array
export const FloatMath = Math

//32
export type Float = f32
export type FloatArray = Float32Array
export const FloatMath = Mathf

@StEvUgnIn
Copy link
Author

Hello @mrchantey,

Please you are welcome for trying and sharing your proposition.

@StEvUgnIn
Copy link
Author

I didn't make any advancement on this PR. I was waiting for as-bind to support Array/Float32Array/Float64Array before going any further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants