bytecodealliance / ComponentizeJS

JS -> WebAssembly Component
Apache License 2.0
241 stars 32 forks source link

Resource function names should be kebab-cased #145

Closed cataggar closed 3 weeks ago

cataggar commented 1 month ago

I've been trying to make my first resource definition. I keep getting an error message that my compilerOptions resource function is not valid.

$ bunx jco componentize cowsay.js --wit src/cowsay.wit -o cowsay.wasm
(jco componentize) ComponentError: failed to encode a component from module
$failed to decode world from module

Caused by:
    0: module was not valid
    1: import of `[resource-new]compilerOptions` is not a valid resource function
    at componentNew (file:///Users/cataggar/ms/jco/obj/wasm-tools.js:3714:11)
    at componentNew (file:///Users/cataggar/ms/jco/src/api.js:37:10)
    at async componentize (file:///Users/cataggar/ms/ComponentizeJS/src/componentize.js:381:5)
    at async componentize (file:///Users/cataggar/ms/jco/src/cmd/componentize.js:11:25)
    at async file:///Users/cataggar/ms/jco/src/jco.js:200:9
error: script "componentize" exited with code 1

Here is my cowsay.wit where I'm trying to define an empty resource compiler-options:

package local:cowsay;

world cowsay {
  export test: func() -> string;
  export canvas: interface {
    ts-version: func() -> string;
    resource compiler-options {
      // constructor();
    }
  }
}

Here is my cowsay.ts where I'm trying to define it:

import * as ts from 'typescript';
export function test() {
    return "ok";
}

class CompilerOptions {
    inner;
    constructor() {
        this.inner = 3;
    }
}

function tsVersion() {
    return ts.version;
}

export const canvas = {
    tsVersion: tsVersion,
    compilerOptions(){
        return new CompilerOptions();
    }
    // compilerOptions: (): CompilerOptions => {
    //     return new CompilerOptions();
    // }
};
guybedford commented 1 month ago

I think you need to export the resource class explicitly on the canvas interface like:

export const canvas = { tsVersion, CompilerOptions };
cataggar commented 1 month ago

Unfortunately, it is still failing with not a valid resource function:

$ bunx jco componentize cowsay.js --wit src/cowsay.wit -o cowsay.wasm
(jco componentize) ComponentError: failed to encode a component from module
$failed to decode world from module

Caused by:
    0: module was not valid
    1: import of `[resource-new]compilerOptions` is not a valid resource function
    at componentNew (file:///Users/cataggar/ms/jco/obj/wasm-tools.js:3714:11)
    at componentNew (file:///Users/cataggar/ms/jco/src/api.js:37:10)
    at async componentize (file:///Users/cataggar/ms/ComponentizeJS/src/componentize.js:381:5)
    at async componentize (file:///Users/cataggar/ms/jco/src/cmd/componentize.js:11:25)
    at async file:///Users/cataggar/ms/jco/src/jco.js:200:9
error: script "componentize" exited with code 1

The cowsay.wit is the same, but I updated cowsay.ts to be:

// import * as ts from 'typescript';
export function test() {
    return "ok";
}

class CompilerOptions {
    inner;
    constructor() {
        this.inner = 3;
    }
}

function tsVersion() {
    // return ts.version;
    return "5.2";
}

export const canvas = {
    tsVersion,
    CompilerOptions
};

Is there something incompatible with the generated JavaScript?

// src/cowsay.ts
function test() {
  return "ok";
}
function tsVersion() {
  return "5.2";
}

class CompilerOptions {
  inner;
  constructor() {
    this.inner = 3;
  }
}
var canvas = {
  tsVersion,
  CompilerOptions
};
export {
  test,
  canvas
};
guybedford commented 1 month ago

I'm wondering if this is a camel casing issue - if you use a resource name that is a single word, like Compileroptions instead, does it work then?

cataggar commented 1 month ago

Yes, that is it. Thank you!

guybedford commented 1 month ago

Thanks for confirming, filed as a ComponentizeJS bug.

cataggar commented 1 month ago

I was reviewing code this evening. Is crates/spidermonkey-embedding-splicer/src/bindgen.rs the right place for this functionality?

guybedford commented 1 month ago

It should be, yes.

cataggar commented 3 weeks ago

I wasn't unable to hunt this one down. Would it help if I attached the generated sources?

guybedford commented 3 weeks ago

Fix in https://github.com/bytecodealliance/ComponentizeJS/pull/151.