VTK  9.7.20260904
vtkWebAssemblySessionHelper.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef vtkWebAssemblySessionHelper_h
5#define vtkWebAssemblySessionHelper_h
6
7#include <emscripten/val.h>
8
9#include "vtkABINamespace.h"
10#include "vtkObjectManager.h"
11#include "vtkSession.h"
12
13VTK_ABI_NAMESPACE_BEGIN
14
15// Implement vtkSessionJsonImpl as a wrapper around emscripten::val
17{
18 emscripten::val JsonValue;
19};
20
21namespace
22{
23
24using namespace emscripten;
25
26// Initialize JavaScript global objects.
27thread_local const val Uint8Array = val::global("Uint8Array");
28thread_local const val Uint32Array = val::global("Uint32Array");
29thread_local const val JSON = val::global("JSON");
30thread_local const val Array = val::global("Array");
31
41vtkSession NewVTKInterfaceForJavaScript()
42{
43 vtkSessionDescriptor descriptor;
44 descriptor.StringifyJson = +[](vtkSessionJson inputJson) -> char*
45 {
46 const auto jsonString = JSON.call<val>("stringify", inputJson->JsonValue).as<std::string>();
47 const auto length = jsonString.length();
48 char* result = new char[length + 1];
49 snprintf(result, length + 1, "%s", jsonString.c_str());
50 return result;
51 };
52 descriptor.ParseJson = +[](const char* inputString) -> vtkSessionJson
53 {
55 result->JsonValue = JSON.call<val>("parse", std::string(inputString));
56 return result;
57 };
58 descriptor.InteractorManagesTheEventLoop = false;
59 return vtkCreateSession(&descriptor);
60}
61
62}
63VTK_ABI_NAMESPACE_END
64
65#endif
vtkSessionJsonParseFunc ParseJson
Definition vtkSession.h:114
vtkSessionJsonStringifyFunc StringifyJson
Definition vtkSession.h:115
This header file provides the C API for working with standalone/remote visualization applications.
struct vtkSessionImpl * vtkSession
Definition vtkSession.h:91
#define vtkCreateSession
Definition vtkSession.h:123
struct vtkSessionJsonImpl * vtkSessionJson
Definition vtkSession.h:96