node.jstypescriptwebxrassemblyscript

Assembly Script And WebXR


So I have been working with three.js and webXR for a bit of time so far, and wanted to integrate it in assembly script. I know how to get webXR to work in typescript, but when I try to use it in assembly script it errors out with the import. Here is the index.ts code and error:

Index.ts:

// The entry file of your WebAssembly module.
import type { Navigator } from 'webxr';

export function add(a: i32, b: i32): i32 {
    return a + b;
}

Error:

ERROR TS1005: 'from' expected.

import type { Navigator } from 'webxr';
    ~~~~
in assembly/index.ts(2,8)

FAILURE 1 parse error(s)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pad-ar@1.0.0 asbuild:untouched: `asc assembly/index.ts --target debug`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the pad-ar@1.0.0 asbuild:untouched script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\...\AppData\Roaming\npm-cache\_logs\2021-07-23T08_01_08_112Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pad-ar@1.0.0 asbuild: `npm run asbuild:untouched && npm run asbuild:optimized`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the pad-ar@1.0.0 asbuild:untouched script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\...\AppData\Roaming\npm-cache\_logs\2021-07-23T08_05_44_373Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pad-ar@1.0.0 asbuild: `npm run asbuild:untouched && npm run asbuild:optimized`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the pad-ar@1.0.0 asbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

asconfig.json

{
   "targets": {
    "debug": {
     "binaryFile": "build/untouched.wasm",
     "textFile": "build/untouched.wat",
     "sourceMap": true,
     "debug": true
    },
    "release": {
      "binaryFile": "build/optimized.wasm",
      "textFile": "build/optimized.wat",
      "sourceMap": true,
      "optimizeLevel": 3,
      "shrinkLevel": 0,
      "converge": false,
      "noAssert": false
    }
  },
  "options": {}
}

Dependencies:

"dependencies": {
    "@assemblyscript/loader": "^0.19.7",
    "@types/webxr": "^0.2.3",
    "typescript": "^4.3.5",
    "webxr": "0.0.0",
    "webxr-polyfill": "^2.0.3"
},
"devDependencies": {
    "@as-pect/cli": "^6.2.4",
    "@types/node": "^16.4.1",
    "assemblyscript": "^0.19.7"
}

Solution

  • Assemblyscript compiles to Web Assembly, which at this time does not have access to the DOM. The idea is to use Assemblyscript for the game logic and regular javascript / typescript for interacting with the WebXR API.