Skip to content

Commit

Permalink
optimize for template-code bloat for WebAssembly
Browse files Browse the repository at this point in the history
1640551 - 1444942 = 195,609
  • Loading branch information
LanderlYoung committed Mar 12, 2023
1 parent 9b34456 commit 74a72f9
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 213 deletions.
1 change: 0 additions & 1 deletion backend/WebAssembly/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
target_sources(ScriptX PRIVATE
${CMAKE_CURRENT_LIST_DIR}/WasmEngine.cc
${CMAKE_CURRENT_LIST_DIR}/WasmEngine.h
${CMAKE_CURRENT_LIST_DIR}/WasmEngine.hpp
${CMAKE_CURRENT_LIST_DIR}/WasmException.cc
${CMAKE_CURRENT_LIST_DIR}/WasmLocalReference.cc
${CMAKE_CURRENT_LIST_DIR}/WasmNative.cc
Expand Down
141 changes: 141 additions & 0 deletions backend/WebAssembly/WasmEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,145 @@ Local<Object> WasmEngine::getNamespaceForRegister(const std::string_view& nameSp
return scope.returnValue(Local<Value>(ret).asObject());
}

// Native

void WasmEngine::performRegisterNativeClass(
internal::TypeIndex typeIndex, const internal::ClassDefineState* classDefine,
script::ScriptClass* (*instanceTypeToScriptClass)(void*)) {
if (classDefineRegistry_.find(classDefine) != classDefineRegistry_.end()) {
throw Exception("classDefine [" + classDefine->className + "] already registered");
}

StackFrameScope scope;

auto hasInstance = classDefine->hasInstanceDefine();
auto obj = hasInstance ? newConstructor(classDefine) : Object::newObject();

defineStatic(obj, classDefine->staticDefine);

if (hasInstance) {
defineInstance(classDefine, obj);
}

auto ns = getNamespaceForRegister(classDefine->nameSpace);
ns.set(classDefine->className, obj);

classDefineRegistry_.emplace(classDefine, obj);
}

Local<Object> WasmEngine::performNewNativeClass(internal::TypeIndex typeIndex,
const internal::ClassDefineState* classDefine,
size_t size, const Local<script::Value>* args) {
auto it = classDefineRegistry_.find(classDefine);
if (it == classDefineRegistry_.end()) {
throw Exception("classDefine [" + classDefine->className + "] is not registered");
}

StackFrameScope scope;
auto ctor = it->second.get();
auto ret = Object::newObjectImpl(ctor, size, args);
return scope.returnValue(ret);
}

bool WasmEngine::performIsInstanceOf(const Local<script::Value>& value,
const internal::ClassDefineState* classDefine) {
return NativeHelper::getInternalStateClassDefine(value.val_) == classDefine;
}

void* WasmEngine::performGetNativeInstance(const Local<script::Value>& value,
const internal::ClassDefineState* classDefine) {
if (performIsInstanceOf(value, classDefine)) {
return static_cast<void*>(NativeHelper::getInternalStateInstance(value.val_));
}
return nullptr;
}

Local<Object> WasmEngine::newConstructor(const internal::ClassDefineState* classDefine) {
auto ctor = Stack::newFunction(
[](const Arguments& args, void* data, void*) -> Local<Value> {
auto classDefine = static_cast<internal::ClassDefineState*>(data);

Tracer trace(args.engine(), classDefine->className);
void* instance;
if (args.size() == 2 && NativeHelper::isCppNewMark(args[0].val_)) {
// WASM is 32-bit, we could use int32_t to store a pointer.
// we have static assert in WasmEngine.h
instance = reinterpret_cast<void*>(args[1].asNumber().toInt32());
} else {
instance = classDefine->instanceDefine.constructor(args);
if (instance == nullptr) {
throw Exception("can't create class " + classDefine->className);
}
}

NativeHelper::setInternalState(args.thiz().val_, classDefine, instance);
return {};
},
classDefine, nullptr, true);
return Local<Object>(ctor);
}

void WasmEngine::defineInstance(const internal::ClassDefineState* classDefine,
const Local<Object>& obj) {
auto&& instanceDefine = classDefine->instanceDefine;
auto prototype = Object::newObject();

for (auto&& func : instanceDefine.functions) {
StackFrameScope stack;
auto fi = Stack::newFunction(
[](const Arguments& args, void* data0, void* data1) -> Local<Value> {
auto classDefine = static_cast<const internal::ClassDefineState*>(data0);
auto& func = *static_cast<typename internal::InstanceDefine::FunctionDefine*>(data1);

auto ins = verifyAndGetInstance(classDefine, args.thiz().val_);

Tracer trace(args.engine(), func.traceName);
return func.callback(ins, args);
},
classDefine, &func);

prototype.set(func.name, Local<Value>(fi));
}

for (auto&& prop : instanceDefine.properties) {
StackFrameScope stackFrame;

int getter = -1;
int setter = -1;
auto name = String::newString(prop.name);

if (prop.getter) {
getter = Stack::newFunction(
[](const Arguments& args, void* data0, void* data1) -> Local<Value> {
auto& prop = *static_cast<typename internal::InstanceDefine::PropertyDefine*>(data0);
auto classDefine = static_cast<const internal::ClassDefineState*>(data1);

auto ins = verifyAndGetInstance(classDefine, args.thiz().val_);

Tracer trace(args.engine(), prop.traceName);
return prop.getter(ins);
},
&prop, classDefine);
}

if (prop.setter) {
setter = Stack::newFunction(
[](const Arguments& args, void* data0, void* data1) -> Local<Value> {
auto& prop = *static_cast<typename internal::InstanceDefine::PropertyDefine*>(data0);
auto classDefine = static_cast<const internal::ClassDefineState*>(data1);

auto ins = verifyAndGetInstance(classDefine, args.thiz().val_);
Tracer trace(args.engine(), prop.traceName);
prop.setter(ins, args[0]);
return {};
},
&prop, classDefine);
}
NativeHelper::defineProperty(prototype.val_, name.val_, getter, setter);
}

// set the `prototype` property of constructor
obj.set("prototype", prototype);
}

} // namespace script::wasm_backend
33 changes: 16 additions & 17 deletions backend/WebAssembly/WasmEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ScriptXTestFixture;

namespace script::wasm_backend {

static_assert(sizeof(int32_t) == sizeof(void*), "ScriptX currently assume running 32-bit wasm");

class WasmEngine : public ScriptEngine {
// < classDefine, Class >
std::unordered_map<const void*, Global<Object>> classDefineRegistry_{};
Expand Down Expand Up @@ -87,32 +89,29 @@ class WasmEngine : public ScriptEngine {
protected:
~WasmEngine() override;

private:
template <typename T>
void registerNativeClassImpl(const ClassDefine<T>* classDefine);
void performRegisterNativeClass(
internal::TypeIndex typeIndex, const internal::ClassDefineState* classDefine,
script::ScriptClass* (*instanceTypeToScriptClass)(void*)) override;

template <typename T>
Local<Object> newConstructor(const ClassDefine<T>* classDefine);
void* performGetNativeInstance(const Local<script::Value>& value,
const internal::ClassDefineState* classDefine) override;
Local<Object> performNewNativeClass(internal::TypeIndex typeIndex,
const internal::ClassDefineState* classDefine, size_t size,
const Local<script::Value>* args) override;
bool performIsInstanceOf(const Local<script::Value>& value,
const internal::ClassDefineState* classDefine) override;

template <typename T>
void defineInstance(const ClassDefine<T>* classDefine, const Local<Object>& obj);
private:
Local<Object> newConstructor(const internal::ClassDefineState* classDefine);

void defineInstance(const internal::ClassDefineState* classDefine, const Local<Object>& obj);

static Local<Object> getNamespaceForRegister(const std::string_view& nameSpace);

void defineStatic(const Local<Object>& obj, const internal::StaticDefine& define);

static void* verifyAndGetInstance(const void* classDefine, int thiz);

template <typename T>
Local<Object> newNativeClassImpl(const ClassDefine<T>* classDefine, size_t size,
const Local<Value>* args);

template <typename T>
bool isInstanceOfImpl(const Local<Value>& value, const ClassDefine<T>* classDefine);

template <typename T>
T* getNativeInstanceImpl(const Local<Value>& value, const ClassDefine<T>* classDefine);

private:
// helpers
template <typename T>
Expand Down
166 changes: 0 additions & 166 deletions backend/WebAssembly/WasmEngine.hpp

This file was deleted.

1 change: 1 addition & 0 deletions backend/WebAssembly/WasmException.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "../../src/Exception.h"
#include "../../src/Native.hpp"
#include "WasmEngine.h"
#include "WasmReference.hpp"

Expand Down
1 change: 0 additions & 1 deletion backend/WebAssembly/WasmHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "WasmHelper.h"
#include "../../src/Native.hpp"
#include "../../src/Scope.h"
#include "WasmEngine.hpp"
#include "WasmScope.hpp"

namespace script::wasm_backend {
Expand Down
Loading

0 comments on commit 74a72f9

Please sign in to comment.