I want to compile in AssemblyScript (TypeScript) a code that shows a 1, using console.log
.
My file called example.ts contains the following:
console.log("1")
I compile it as follows:
asc example.ts
But I get the following error:
ERROR TS2304: Cannot find name 'console'.
console.log("1")
~~~~~~~
in example.ts(1,1)
FAILURE 1 compile error(s)
The same thing happens with process.stdout.write("1")
.
How is this solved?
I asked for help on AssemblyScript server on Discord and a contributor (dcodeIO) was able to fix a bug about UTF-8 encoding. So for this it is necessary to have AssemblyScript as of version 0.17.7, the version in which this error was fixed. Also, another collaborator (MaxGraey), helped me to create the codes, and also to install the packages.
First you have to install the loader:
In my case I couldn't, I had to install it globally, due to reading problems on the USB where I have the operating system installed:
You have to be careful, there is an
npm
package calledassemblyscript-loader
, notice that it has a minus sign in the center, you don't have to do the following:The code of example.ts has to be as follows:
To compile the example.ts with
asc
, you can do the following ... I add optimizations for less file weight:This generates a file of 3033 bytes, equivalently to 3 kb.
To execute the wasm code from Node.js you have to create a file, for example, example.js, and it would look like the following:
It is important to use
fs.readFileSync("./example.wasm")
, since when using only"./example.wasm"
, it does not work. This is because the implementation of the loader AssemblyScript (which is created in Node.js), in the index.js file approximately on line 333, asks as an argument a variable of typeBuffer
, which are the bytes of the example.wasm.In my case, as I install it globally, in the example.js I changed it to the following:
To run the example.js file from Node.js, it can be done as follows. I put code tracing arguments in case something strange happens:
The output of running the command is as follows:
As you can see, it works correctly.