"TypeError: Module.X is not a constructor" while creating C++ object in javascript code after compilation with intermediate bitcode state

39 views
Skip to first unread message

Pawel

unread,
Sep 4, 2019, 9:38:12 AM9/4/19
to emscripten-discuss
Hello All,

I get a TypeError: Module.ClassA is not a constructor error while creating object.
I'm having a simple header class defined in header class_a.h:
namespace MyNamespace
{
  class ClassA
  {
  public:
    ClassA();
    void PrintName();
  };
}

and its implemetation in class_a.cpp
#include <iostream>
#include "class_a.h"
namespace MyNamespace
{
  ClassA::ClassA()
  {
    std::cout << "MyNamespace::ClassA constructor" << std::endl;
  }
  void ClassA::PrintName()
  {
    std::cout << "MyNamespace::ClassA::PrintName method" << std::endl;
  }
}

I described in interface in class_a.idl:
[Prefix="MyNamespace::"]
interface ClassA
{
  void ClassA();
  void PrintName();
};

Next I create glue files with:
python ${EMSCRIPTEN_ROOT}/tools/webidl_binder.py class_a.idl class_a_glue

And glue_wrapper.cpp containing
#include "class_a.h"
#include "class_a_glue.cpp"

Now I'd like to create bitcode
em++ class_a.cpp glue_wrapper.cpp --post-js class_a_glue.js -o class_a.bc
and finally the javascript
em++ class_a.bc -o class_a.js

Up to now everything works fine, neither error nor warning reported.

I define simle client.js script creating object and calling method:
var obj = new Module.ClassA();
obj.PrintName();

When I load both class_a.jd and client.js scripts in html I get
client.js:1 Uncaught TypeError: Module.ClassA is not a constructor at client.js:1


The interesting is that, if I create javascript in one step (without intermediate .bc) then it works fine - I see proper printouts in browser console.
I'm however not likely going this way because I've got a bunch of classes I'd like to put into one javascript, e.g.

em++ class_a.cpp glue_wrapper.cpp --post-js class_a_glue.js -o class_a.bc
em++ class_b.cpp b_glue_wrapper.cpp --post-js class_b_glue.js -o class_b.bc
em++ class_c.cpp c_glue_wrapper.cpp --post-js class_c_glue.js -o class_c.bc
...
em++ class_a.bc class_b.bc class_c.bc ... -o class_a.js.

I'm using:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.35.23
clang version 3.9.0  (emscripten 1.35.23 : 1.35.23)

Why it does not work with intermediate bitcode step? Or maybe my approach is incorrect?

Best regards, Paweł

Thomas Lively

unread,
Sep 4, 2019, 11:53:12 AM9/4/19
to emscripte...@googlegroups.com
Hi Pawel,

I notice that the emscripten version you’re using is quite old. Can you reproduce the issue with the latest version?
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/fba850f3-944d-4626-a28b-bf1d376b76aa%40googlegroups.com.

Pawel

unread,
Sep 5, 2019, 8:32:54 AM9/5/19
to emscripten-discuss
Hi Thomas,

I reproduce it using version 1.36.14.
I cannot use newer versions (>=1.37.0) yet due to required dependencies I'm obligated not to use in my project.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Enkidu Zero

unread,
Sep 5, 2019, 8:39:36 AM9/5/19
to emscripte...@googlegroups.com
ขอบคุณ!

ส่งจากโทรศัพท์มือถือ Huawei ของฉัน


-------- ข้อความต้นฉบับ --------
เรื่อง: Re: "TypeError: Module.X is not a constructor" while creating C++ object in javascript code after compilation with intermediate bitcode state
จาก: Pawel
ถึง: emscripten-discuss
สำเนา:


To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/b3b0c1e1-74cd-401f-a7d2-093cff32ddea%40googlegroups.com.

Pawel

unread,
Sep 10, 2019, 4:05:22 AM9/10/19
to emscripten-discuss
I've reproduced the issue on separated Ubuntu 19.04 environment using latest available components:
emscripten-1.38.31
clang-e1.38.31-64bit
node-12.9.1-64bit

Is it a bug or my workflow is wrong?
Reply all
Reply to author
Forward
0 new messages