emscripten-core / emscripten-core/emscripten

embind-emit-tsd definitions don't work with Angular

Open
#20,609 2 comments 0 reactions 1 assignee Claimed by @brendandahl View on GitHub
embind
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Please include the following in your bug report:

**Version of emscripten/emsdk:**
3.1.47

**Failing command line in full:**

After running the below script:

```
#!/bin/sh

set -e
set -v

ng new myApp --routing --style=scss

cd myApp

sed -i "/'@angular\/core';/a\
import type { MainModule } from './myWrapperDefs.d.ts';\n\
declare function MyWrapperModule() : Promise;\n\
import MyWrapperModule from './myWrapperModule.js';" src/app/app.component.ts

sed -i '/title = /a\
constructor()\
{\
console.log("INVOKED CTOR");\
MyWrapperModule().then(module => {\
console.log("GOT MODULE");\
});\
}' src/app/app.component.ts

sed -i '/noImplicitReturns/a "noImplicitAny": false,' tsconfig.json

git diff

cd ..

cat << EOF > mywrapper.cpp

#include
#include
#include

class MyWrapper
{
public:
explicit MyWrapper();
~MyWrapper();

int getNum();
};

#include

#include

int main(int argc, char** argv)
{
return 0;
}

MyWrapper::MyWrapper()
{
}

MyWrapper::~MyWrapper()
{
}

int MyWrapper::getNum()
{
return 42;
}

// clang-format off
EMSCRIPTEN_BINDINGS(my_wrapper)
{
emscripten::class_("MyWrapper")
.constructor()
.function("getNum", &MyWrapper::getNum)
;
}
// clang-format on
EOF

em++ --embind-emit-tsd=myWrapperDefs.d.ts -O2 -g -o mywrapper.cpp.o -c mywrapper.cpp
em++ --embind-emit-tsd=myWrapperDefs.d.ts -O2 -g -sWASM=1 \
-sMODULARIZE=1 -sEXPORT_NAME=MyWrapperModule \
--minify=0 --bind \
-sENVIRONMENT=web mywrapper.cpp.o -o myWrapperModule.js

mv myWrapperDefs.d.ts myWrapperModule.js myWrapperModule.wasm myApp/src/app

cd myApp
ng build
ng serve

```

then open http://localhost:4200/ and open devtools to the the output. The output is

```
app.component.ts:15 INVOKED CTOR
ERROR ReferenceError: MyWrapperModule is not defined
at new AppComponent (app.component.ts:16:5)
at NodeInjectorFactory.AppComponent_Factory [as factory] (app.component.ts:19:4)
at getNodeInjectable (core.mjs:4862:44)
at createRootComponent (core.mjs:14273:35)
at ComponentFactory.create (core.mjs:14137:25)
at ApplicationRef.bootstrap (core.mjs:28701:42)
at core.mjs:28411:64
at Array.forEach ()
at PlatformRef._moduleDoBootstrap (core.mjs:28411:44)
at core.mjs:28381:26
```

I realize this might be an angular bug, but I'm continuing to try to use the output of embind-emit-tsd and I can not get it to work.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.