emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

webidl JS Implementation function arguments passed as value instead of pointer/reference by default

Open martinbirks opened this issue 8 years ago • 3 comments

The arguments for JSImplementation seem to be passed by value if they are namespaced, for example with the following webIdl file, I would expect Callback(Foo foo) to have Foo passed as a pointer:


[Prefix = "TestNamespace::"]
interface Foo
{
};

[Prefix = "TestNamespace::"]
interface BarBase
{
};

[JSImplementation = "BarBase"]
interface Bar
{
	void Callback(Foo foo);
};

This generates the following bar class in C++:

class Bar : public TestNamespace::BarBase {
public:
  void Callback(TestNamespace::Foo arg0) {
    EM_ASM_INT({
      var self = Module['getCache'](Module['Bar'])[$0];
      if (!self.hasOwnProperty('Callback')) throw 'a JSImplementation must implement all functions, you forgot Bar::Callback.';
      self['Callback']($1);
    }, (int)this, (int)arg0);
  }
  void __destroy__() {
    EM_ASM_INT({
      var self = Module['getCache'](Module['Bar'])[$0];
      if (!self.hasOwnProperty('__destroy__')) throw 'a JSImplementation must implement all functions, you forgot Bar::__destroy__.';
      self['__destroy__']();
    }, (int)this);
  }
};

Test.cpp.txt Test.idl.txt

I think the issue is with the namespace. I can fix this in my local copy with webidle_binder.py in the type_to_cdec(raw) function (currently line 337 in trunk). The issue seems to be it is never finding name in interfaces and so always hitting: if name not in interfaces: return ret

If this is changed to the following then the code generates as expected: if raw.type.name not in interfaces: return ret

See attached files for existing and expected results Test_Expected.cpp.txt

martinbirks avatar Nov 01 '18 12:11 martinbirks

Proposed fix here: #7426

martinbirks avatar Nov 01 '18 12:11 martinbirks

76f6fac3bf943f1dac3e0cf3a4d1aa28af2b0f40

martinbirks avatar Nov 08 '18 08:11 martinbirks

why not wrap pointer as excepted Object, as following self['Callback'](wrapPointer($1,Foo)); and modify the corresponding place in webidl.py to generate code automatically

legend-chen avatar Nov 28 '18 06:11 legend-chen