-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now building wasm target of Realm Core
- Loading branch information
1 parent
43a9da0
commit 0058237
Showing
10 changed files
with
144 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#pragma once | ||
|
||
#include <emscripten.h> | ||
#include <realm_helpers.h> | ||
|
||
namespace realm::js::wasm { | ||
namespace { | ||
|
||
REALM_NOINLINE inline emscripten::val toEmscriptenErrorCode(const std::error_code& e) noexcept | ||
{ | ||
REALM_ASSERT_RELEASE(e); | ||
auto jsErr = emscripten::val::global("Error")(emscripten::val(e.message())); | ||
jsErr.set("code", e.value()); | ||
jsErr.set("category", e.category().name()); | ||
|
||
return jsErr; | ||
} | ||
REALM_NOINLINE inline emscripten::val toEmscriptenException(const std::exception& e) noexcept | ||
{ | ||
return emscripten::val::global("Error")(std::string(e.what())); | ||
} | ||
REALM_NOINLINE inline emscripten::val toEmscriptenException(const std::exception_ptr& e) noexcept | ||
{ | ||
try { | ||
std::rethrow_exception(e); | ||
} | ||
catch (const std::exception& e) { | ||
return toEmscriptenException(e); | ||
} | ||
catch (...) { | ||
return emscripten::val::global("Error")(std::string("Unknown error")); | ||
} | ||
} | ||
// Allocate a new C++ buffer big enough to fit the JS buffer | ||
// Create a JS memory view around the C++ buffer | ||
// Call TypedArray.prototype.set to efficiently copy the JS buffer into the C++ buffer via the view | ||
REALM_NOINLINE inline std::string toBinaryData(const emscripten::val array_buffer) noexcept | ||
{ | ||
REALM_ASSERT(array_buffer.instanceof (emscripten::val::global("ArrayBuffer"))); | ||
std::string buf; | ||
buf.resize(array_buffer["byteLength"].as<int32_t>()); | ||
|
||
emscripten::val mv(emscripten::typed_memory_view(buf.length(), buf.data())); | ||
mv.call<void>("set", emscripten::val::global("Uint8Array").new_(array_buffer)); | ||
|
||
return buf; | ||
} | ||
REALM_NOINLINE inline OwnedBinaryData toOwnedBinaryData(const emscripten::val array_buffer) noexcept | ||
{ | ||
REALM_ASSERT(array_buffer.instanceof (emscripten::val::global("ArrayBuffer"))); | ||
auto length = array_buffer["byteLength"].as<int32_t>(); | ||
|
||
std::unique_ptr<char[]> buf(new char[length]); | ||
|
||
emscripten::val mv(emscripten::typed_memory_view(length, buf.get())); | ||
mv.call<void>("set", emscripten::val::global("Uint8Array").new_(array_buffer)); | ||
|
||
return OwnedBinaryData(std::move(buf), length); | ||
} | ||
} // namespace | ||
} // namespace realm::js::wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 Realm Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
import { injectAndPatch } from "../binding"; | ||
const bindingPromise = import("../../../binding/generated/native.wasm.cjs"); | ||
|
||
bindingPromise.then(injectAndPatch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters