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
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
65f3513
configuring env for working with assemblyscript
StEvUgnIn Mar 26, 2021
600fc7d
porting js into as
StEvUgnIn Mar 26, 2021
68c0b01
set assemblyscript portability for AS/TS
StEvUgnIn Mar 27, 2021
758766d
update gitignore - yarn
StEvUgnIn Mar 27, 2021
2239143
configuration for compilation of AS into JS
StEvUgnIn Mar 27, 2021
b61761e
add source types declarations for build
StEvUgnIn Mar 27, 2021
4276d9c
fix required explicit toString conversion
StEvUgnIn Mar 28, 2021
fd1907b
fix module typings
StEvUgnIn Mar 28, 2021
f917c0c
implement workaround for AS100
StEvUgnIn Mar 28, 2021
2e2ac0b
tsc build dist/index.dts from portable AS directly
StEvUgnIn Mar 28, 2021
a53c4e3
build successfully javascript and webassembly
StEvUgnIn Mar 28, 2021
6f18cce
removed directive for TS typings
StEvUgnIn Mar 29, 2021
f1b573b
another fix to AS100 for expected closure
StEvUgnIn Mar 29, 2021
6ff09f6
fix AS warnings
StEvUgnIn Mar 29, 2021
6deca79
optimize MathUtil library
StEvUgnIn Mar 29, 2021
dc5e7da
optimize build for speed
StEvUgnIn Mar 29, 2021
cca26fa
fix modules in dist/index.d.ts
StEvUgnIn Mar 29, 2021
598eff0
compile from single file index.ts
StEvUgnIn Mar 30, 2021
28f978a
changetype is not implemented for TS compilation
StEvUgnIn Mar 30, 2021
0749891
change MathUtil into JSMath and Maths namespaces
StEvUgnIn Mar 30, 2021
36ecee9
merge JSMath and Maths and toolchain TS into AMD modules
StEvUgnIn Apr 1, 2021
79a3b97
simplify toolchain to compile TS/AS into JS
StEvUgnIn Apr 1, 2021
8a56b64
definitive toolchain compilation
StEvUgnIn Apr 1, 2021
78a6d91
hypot cannot be parsed in JS build
StEvUgnIn Apr 2, 2021
0dd49ae
javascript interface for webassembly
StEvUgnIn Apr 2, 2021
194ed13
update .travis.yml for using latest node-lts version
StEvUgnIn Apr 2, 2021
1922b8d
add upper case support
StEvUgnIn Apr 4, 2021
1124e84
right asynchronous call for WebAssembly loading
StEvUgnIn Apr 4, 2021
885e3b1
update assembly and asconfig.json
StEvUgnIn Apr 26, 2021
2b03776
loader
StEvUgnIn Apr 26, 2021
32801c0
build
StEvUgnIn Apr 28, 2021
ae420e4
as-pect
StEvUgnIn Apr 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"gzipped": 13273
},
"dist\\gl-matrix-min.js": {
"bundled": 54440,
"minified": 54369,
"gzipped": 13854
"bundled": 54568,
"minified": 54478,
"gzipped": 13921
}
}
3 changes: 3 additions & 0 deletions assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "assemblyscript/std/portable.json",
"exclude": [
"./**/*.spec.ts"
],
"include": [
"./**/*.ts"
]
Expand Down
6 changes: 3 additions & 3 deletions build/loader-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import wasm from './untouched.wasm';

let modules;

wasm({ ...imports }).then(({ instance }) => {
modules = instance.exports
})
wasm({ ...imports }).then(instance => {
modules = instance.exports;
});

export const {
glMatrix,
Expand Down
6 changes: 3 additions & 3 deletions build/loader-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import wasm from './optimized.wasm';

let modules;

wasm({ ...imports }).then(({ instance }) => {
modules = instance.exports
})
wasm({ ...imports }).then(instance => {
modules = instance.exports;
});

export const {
glMatrix,
Expand Down
2 changes: 2 additions & 0 deletions loader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js
!index.js
14 changes: 14 additions & 0 deletions loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import wasm from '../build/optimized.wasm';

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

modules = instance.exports;
});

export const {
glMatrix,
mat2, mat2d, mat3, mat4,
quat, quat2,
vec2, vec3, vec4
} = modules;
9 changes: 9 additions & 0 deletions loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "umd",
"outDir": "../dist/wasm",
"allowJs": true,
"checkJs": false
},
"exclude": ["index.js"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"build-esm": "tsc --module es6 assembly/index.ts --outDir dist/esm",
"build-cjs": "tsc --module commonjs assembly/index.ts -outDir dist/cjs",
"build-dts": "tsc --declaration --emitDeclarationOnly --module amd --outFile ./dist/index.d.ts ./assembly/index.ts && node ./utils/bundle-dts.js && tsc --noEmit ./dist/index.d.ts",
"build": "del dist && npm run update-license-version && npm run asbuild && npm run build-umd && npm run build-esm && npm run build-cjs && npm run build-dts && node ./utils/build.js",
"build-loader": "node ./utils/build-loader.js && tsc -p loader/tsconfig.json",
"build": "del dist && npm run update-license-version && npm run build-loader && npm run asbuild && npm run build-umd && npm run build-esm && npm run build-cjs && npm run build-dts && node ./utils/build.js",
"prepare": "npm run build",
"asbuild:untouched": "asc assembly/index.ts --target debug",
"asbuild:optimized": "asc assembly/index.ts --target release",
Expand Down
16 changes: 12 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ ${license}
}

export default [
{
input: './loader/index.js',
output: { file: 'dist/wasm/index.js', format: 'umd', name },
plugins: [
replace({ preventAssignment: true }),
wasm({ sourceMap: true }),
]
},
{
input: './build/loader-debug.js',
output: { file: 'dist/wasm/gl-matrix-loader-debug.js', format: 'umd', name },
output: { file: 'dist/gl-matrix-loader-debug.js', format: 'umd', name },
plugins: [
replace({ preventAssignment: true }),
wasm({ sourceMap: true }),
Expand All @@ -34,7 +42,7 @@ export default [
},
{
input: './build/loader-release.js',
output: { file: 'dist/wasm/gl-matrix-loader-release.js', format: 'umd', name },
output: { file: 'dist/gl-matrix-loader-release.js', format: 'umd', name },
plugins: [
replace({ preventAssignment: true }),
wasm({ sourceMap: true }),
Expand All @@ -46,7 +54,7 @@ export default [
output: { file: 'dist/gl-matrix.js', format: 'umd', name },
plugins: [
replace({ preventAssignment: true }),
typescript(),
typescript({ tsconfig: 'assembly/tsconfig.json'}),
bannerPlugin
]
},
Expand All @@ -55,7 +63,7 @@ export default [
output: { file: 'dist/gl-matrix-min.js', format: 'umd', name },
plugins: [
replace({ preventAssignment: true }),
typescript(),
typescript({ tsconfig: 'assembly/tsconfig.json'}),
terser({
output: { comments: /^!/ }
}),
Expand Down
44 changes: 44 additions & 0 deletions utils/build-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require('fs');
const path = require('path');

const inputDir = 'assembly';
const outputDir = 'loader';

let declarations = new Set();

let content;

fs.readdirSync(inputDir)
.filter(file =>
file.includes('.ts') &&
!file.includes('import') &&
!file.includes('index') &&
!file.includes('math') &&
!file.includes('d.ts') &&
!file.includes('_tests_'))
.forEach(file => {
const fileName = file.split('.')[0];
let moduleName = fileName;
if (file.includes('common')) {
moduleName = 'glMatrix';
}
content = `import { ${moduleName} } from './index.js'\n\nexport const {\n`;
console.log(`Searching for exported functions and objects in file ${path.join(inputDir, file)}...`);
fs.readFileSync(path.join(inputDir, file)).toString().split('\n').forEach(line => {
if (line.match(/export.+(function|var).[a-z]+.+[;\{]/g) !== null) {
declarations.add(line.replace(/export.+(function|var).([a-z]+).+[;\{\r]/g, "$2"));
}
});

declarations.forEach((value, value2, set) => {
if (value != 'min' && value != 'max') {
content += ` ${value},\n`;
}
});
content += `\} = ${moduleName};\n`;

console.log(`Found ${declarations.size ?? 0} exports.`);

loader = fs.writeFileSync(path.join(outputDir, fileName + '.js'), content);
console.log(`Exports are being written to file ${path.join(outputDir, fileName + '.js')}\n`);
});
10 changes: 8 additions & 2 deletions utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ copyFileSync('README.md', 'dist/README.md');
copyFileSync('LICENSE.md', 'dist/LICENSE.md');

const files = fs.readdirSync('assembly')
.filter(file => !file.includes('common') && !file.includes('index'))
.filter(file =>
!file.includes('_tests_') &&
!file.includes('.json') &&
!file.includes('import') &&
!file.includes('math') &&
!file.includes('common') && !file.includes('index'))
.forEach(file => {
const name = file.endsWith('.js') ? file.slice(0, -3) : file;
const name = file.endsWith('.ts') ? file.slice(0, -3) : file;
file = file.slice(0, -3) + '.js';
const filePkg = {
name: `gl-matrix/${name}`,
main: `../cjs/${file}`,
Expand Down