-------------------------------------------------------------------------------
| 118 | |
| 119 | //------------------------------------------------------------------------------- |
| 120 | emscripten::val vtkRemoteSession::Invoke( |
| 121 | vtkTypeUInt32 object, const std::string& methodName, emscripten::val args) |
| 122 | { |
| 123 | if (!args.instanceof (Array)) |
| 124 | { |
| 125 | vtkLog( |
| 126 | ERROR, << "Invoke must be called with an objectId: u32, methodName: string, args: Array"); |
| 127 | return emscripten::val::undefined(); |
| 128 | } |
| 129 | if (auto* manager = static_cast<vtkObjectManager*>(vtkSessionGetManager(this->Session))) |
| 130 | { |
| 131 | if (auto* dataArray = vtkDataArray::SafeDownCast(manager->GetObjectAtId(object))) |
| 132 | { |
| 133 | if (methodName == "SetArray") |
| 134 | { |
| 135 | if (args["length"].as<std::size_t>() == 1) |
| 136 | { |
| 137 | auto jsArray = args[0]; |
| 138 | for (const auto& it : IsJSArraySameTypeAsVtkDataArray) |
| 139 | { |
| 140 | const auto& typeName = it.first; |
| 141 | if (jsArray.instanceof (val::global(typeName.c_str())) && it.second(dataArray)) |
| 142 | { |
| 143 | const auto length = jsArray["length"].as<std::size_t>(); |
| 144 | dataArray->SetNumberOfValues(length); |
| 145 | // Copy the data from the JS array to the VTK data array |
| 146 | using DispatchT = vtkArrayDispatch::DispatchByValueType<vtkArrayDispatch::AllTypes>; |
| 147 | if (!DispatchT::Execute(dataArray, CopyJSArrayToVTKDataArray{}, jsArray)) |
| 148 | { |
| 149 | // Fallback to the default implementation if the DispatchT fails |
| 150 | CopyJSArrayToVTKDataArray{}(dataArray, jsArray); |
| 151 | } |
| 152 | return emscripten::val::undefined(); |
| 153 | } |
| 154 | } |
| 155 | vtkLog(ERROR, |
| 156 | "Unsupported argument constructed by " |
| 157 | << jsArray["constructor"].call<std::string>("toString") << " for " |
| 158 | << dataArray->GetClassName() << "::SetArray" |
| 159 | << " method."); |
| 160 | return emscripten::val::undefined(); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | vtkLog(ERROR, << "vtkDataArray::SetArray expects a list of a single TypedArray"); |
| 165 | return emscripten::val::undefined(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | vtkLog(ERROR, "Invalid session: " << this->Session); |
| 173 | return emscripten::val::undefined(); |
| 174 | } |
| 175 | vtkSessionJsonImpl argsJsonImpl{ args }; |
| 176 | auto resultImpl = vtkSessionInvoke(this->Session, object, methodName.c_str(), &argsJsonImpl); |
| 177 | auto result = std::move(resultImpl->JsonValue); |
nothing calls this directly
no test coverage detected