Calling a TypeScript method from C++ using Emscripten

88 views
Skip to first unread message

Aditya Das

unread,
Jul 26, 2023, 10:43:53 AM7/26/23
to emscripten-discuss
The following is my TypeScript code:
````typescript
export class VtkWasmRenderComponent extends EmscriptenWasmComponent implements AfterViewInit, OnDestroy {
 .....
 .....
 .....

  vtkCameraChanged(cameraChangeEvent: CameraChangeEvent) {
    if (cameraChangeEvent === this.lastCameraChangeEvent) return;
    this.lastCameraChangeEvent = cameraChangeEvent;
    this.cameraChange.next(cameraChangeEvent);
  }
}
````
In my C++ file, I have the following:
````cpp
using namespace emscripten;
/*
extern "C" {
  extern void vtkCameraChanged(CameraChangeEvent positionChange);
}
*/

void vtkCallbackFunc(vtkObject* caller, long unsigned int evId,
                     void* clientData, void* /*callData*/)
{
  // Note the use of reinterpret_cast to cast the caller and callData to the
  // expected types.
  auto interactor = reinterpret_cast<vtkRenderWindowInteractor*>(caller);
  std::cout << "  Event Id: " << std::endl;
  auto renderer = reinterpret_cast<vtkRenderer*>(clientData);
   CameraChangeEvent positionChange;
    positionChange.position = renderer->GetActiveCamera()->GetPosition();
    positionChange.viewAngle = renderer->GetActiveCamera()->GetViewAngle();
    positionChange.clippingRange = renderer->GetActiveCamera()->GetClippingRange();
    positionChange.focalPoint = renderer->GetActiveCamera()->GetFocalPoint();
    positionChange.parallelProjection = renderer->GetActiveCamera()->GetParallelProjection();
    positionChange.parallelProjection = renderer->GetActiveCamera()->GetParallelProjection();
    positionChange.viewUp = renderer->GetActiveCamera()->GetViewUp();
    positionChange.parallelScale = renderer->GetActiveCamera()->GetParallelScale();
    val vtkWasm = val::global("VtkWasmRenderComponent").new_();
    vtkWasm.call<void>("vtkCameraChanged", positionChange); // calling the vtkCameraChange function
   
};

````

What is the correct way to call the the `vtkCameraChange` method in the TypeScript file from the C++ file using emscripten? I have used val as stated in the [documentation](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-call-javascript-from-native), however, it does not seem to work.
Reply all
Reply to author
Forward
0 new messages