WASM and Node.js

When I build a wasm project I get MyLibrary.a and MyLibrary.fx in wasm32 folder. Looking in Node.js docs it says I am supposed to have a *.wasm file.

Using the files from Elements I get:

[CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 21 3c 61 72 @+0]

How am I supposed to use these files, can I use them with Node.js?

An .a is a Static Library. you would use that to compile reusable objects you wanna use in other projects (much like Element RTL is Elements.a).

To get a runnable Wasm project (Node.js for Browser) you’d want to build a (non-static) module, a Librray.

The WebAssembly Module project templates should generate the right kind of output.

If I understand correctly, Shared Project and Multi-Target Library belong to:

Screenshot 2022-06-12 at 17.03.35

Tab.

And even though I am visiting other tabs like Cocoa, they are still Shared?


Yes Im using those templates and I get appropriate outputs

It’s very cool. With the Node.js Module I can declare variables (C# syntax sugar), in Program class:

public string ProjectName { get; } = "NodeJsModuleProject";
public string SharedProjectName => new Library2.Class1().MyText; // referenced

Screenshot 2022-06-12 at 17.34.52

export interface Program{
    ProjectName: any;
    SharedProjectName: any;
}

They both work to print by calling the built entry.js file with node. Super!

Two other things. It does not seem possible to declare static variables, and also I noticed type is set to any. Is to possible to achieve types?

So it seems I don’t have to create code to call the *.wasm file itself but use the built generated js code around it

Technically, these two apply to any platform, which is why we show them in all tabs, not just the shared one. e.g. you could use a Sharer Project, even if you only ever code for .NET — there’s nothing inherently cross-platform about them, even though they can also be sued to share code between two or more platforms…

Cool.

:raised_hands:t3:

Static variables/fields should work with the standard “static” C# keyword.

As for the “any”, I believe that’s just the nature of JavaScript not being typed, on the JS side these classes come thru as JavaScript objects that yo can (try to) call anything on, they will not be strongly-typed as they are on the C# side…

Static property declared in Fire with C# don’t show up in the Module.d.ts

export declare class Module {
    instance: any;
    module: any;
    private constructor();
    static instantiate(url?: string): Promise<Module>;

    Library2_Class1(): Library2_Class1;
    Program(): Program;
}
export interface Library2_Class1{
    MyText: any;
}

export interface MyWrappedString{
    Value: any;
}

export interface Program{
    ProjectName: any;
    SharedProjectName: any;
    WrappedString: MyWrappedString;
}

Also I notice I can wrap primitive types in classes which does get declared.

I hope I somehow can get full typescript typings from the Elements project though

enum and int don’t get declared and end up being undefined when trying to print it (in Node)

I guess it makes sense everything has to be turned into text?

// Elements
public String EnumTestProperty { get; } = EnumTest.Three.ToString();
public String TestInt { get; } = 12.ToString();
public object TestObject { get; } = "".ToString();

Ah, i see. reproduced; it’s not in the interface, but its also not callable,

        public string Foo { get { return "foo"; } }
        public static string Bar { get { return "bar"; } }

        console.log(page.Foo)
        console.log(page.Bar)

gives “Undefined” for Bar. i’m not sure if this isn’t as designed though, i’m not familiar with JavaScript and its patterns for static members; I’ll have to check with the team.

Logged as bugs://E25946.

Hmm,

    public int Baz { get { return 5; } }

works fine for me from JS?

Correct I probably misspelled. It did still not show up in Module.d.ts though (int), while string, dictionary, callback (Action) does, with any type.

Curious. jic, can i see a test case for this?

Sure here the playground project I am using:

Library2.zip (2.8 MB)

I see

export interface Program extends RemObjects_Elements_System_Object{
    HelloWorld();
    ProjectName: any;
    SharedProjectName: any;
    WrappedString: MyWrappedString;
    ProjectNameName: any;
    ProjectNameNameName: any;
    EnumTestProperty: any;
    TestIntA: number;
    TestIntB: number;
    TestObject: RemObjects_Elements_System_Object;
    MyBool: boolean;
}

for

        public string ProjectName { get; } = "NodeJsModuleProject";

        public string SharedProjectName => new Library2.Class1().MyText;

        public static string MyStaticString { get; } = "MyStaticString";
        public MyWrappedString WrappedString { get; } = new MyWrappedString();

        public string ProjectNameName { get; } = "Nodoject";
        public string ProjectNameNameName { get; } = "Nodojasas";

        public String EnumTestProperty { get; } = EnumTest.Three.ToString();
        public int TestIntA { get; } = 1; // <-----
        public int TestIntB { get { return 2; } } // <----
        public object TestObject { get; } = "".ToString();

        public bool MyBool {get;} = true;

        public void HelloWorld()
        {
            writeLn("Hello World from Node.js");
            WebAssembly.Eval("console.log(\"Hello via eval\")");
        }

which all looks good?

I updated Fire, I still don’t get the type declaration. Here is the setup I have:

I get:

export interface Program{
    ProjectName: any;
    SharedProjectName: any;
    WrappedString: MyWrappedString;
    ProjectNameName: any;
    ProjectNameNameName: any;
    EnumTestProperty: any;
}

Where are you looking? When I build your project, in Library2\Module\Bin\Debug\WebAssembly\wasm32 I see module.d.ts with the same definition as marc has?

That’s very strange. I removed project, downloaded the zip. Built.

In that folder, I still get C# int variables missing, also the boolean one in the declaration:

export interface Program{
    ProjectName: any;
    SharedProjectName: any;
    WrappedString: MyWrappedString;
    ProjectNameName: any;
    ProjectNameNameName: any;
    EnumTestProperty: any;
}

What’s the problem with that? that looks correct?

Yes, I just pasted it to clarify it was the same as ck’s

Could it be the Mono version or something?

Unlikely. But aside from what the .d.ts looks like, what is the actual concrete problem? What piece of code does not work?