-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
1 deletion.
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,47 @@ | ||
// | ||
// Created by Marc Rousavy on 21.02.24. | ||
// | ||
|
||
#include "HybridObject.h" | ||
|
||
namespace margelo { | ||
|
||
HybridObject::~HybridObject() { | ||
_functionCache.clear(); | ||
} | ||
|
||
|
||
|
||
jsi::Value HybridObject::get(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) { | ||
std::string name = propName.utf8(runtime); | ||
auto& functionCache = _functionCache[&runtime]; | ||
|
||
if (functionCache.count(name) > 0) { | ||
// cache hit | ||
return jsi::Value(runtime, *functionCache[name]); | ||
} | ||
|
||
if (_methods.count(name) > 0) { | ||
// cache miss - create jsi::Function and cache it. | ||
jsi::HostFunctionType hostFunction = _methods.at(name); | ||
jsi::Function function = jsi::Function::createFromHostFunction(runtime, | ||
jsi::PropNameID::forUtf8(runtime, name), | ||
// TODO(marc): Also add parameter count here | ||
1, | ||
hostFunction); | ||
functionCache[name] = std::make_shared<jsi::Function>(std::move(function)); | ||
return jsi::Value(runtime, *functionCache[name]); | ||
} | ||
|
||
return jsi::HostObject::get(runtime, propName); | ||
} | ||
|
||
std::vector<jsi::PropNameID> HybridObject::getPropertyNames(facebook::jsi::Runtime& runtime) { | ||
std::vector<jsi::PropNameID> result; | ||
for (const auto& item: _methods) { | ||
result.push_back(jsi::PropNameID::forUtf8(runtime, item.first)); | ||
} | ||
return result; | ||
} | ||
|
||
} // margelo |
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,71 @@ | ||
// | ||
// Created by Marc Rousavy on 21.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <unordered_map> | ||
#include <jsi/jsi.h> | ||
|
||
#include "JsiTypeTraits.h" | ||
|
||
namespace margelo { | ||
|
||
using namespace facebook; | ||
|
||
class HybridObject: public jsi::HostObject { | ||
struct HybridFunction { | ||
jsi::HostFunctionType function; | ||
size_t parameterCount; | ||
}; | ||
|
||
private: | ||
std::unordered_map<std::string, HybridFunction> _methods; | ||
std::unordered_map<jsi::Runtime*, std::unordered_map<std::string, std::shared_ptr<jsi::Function>>> _functionCache; | ||
|
||
public: | ||
~HybridObject(); | ||
|
||
jsi::Value get(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override; | ||
std::vector<jsi::PropNameID> getPropertyNames(facebook::jsi::Runtime& runtime) override; | ||
|
||
virtual void loadMethods() = 0; | ||
|
||
|
||
private: | ||
template<typename ArgType> | ||
ArgType convertArgument(jsi::Runtime& runtime, const jsi::Value& arg) { | ||
// Implement conversion from jsi::Value to ArgType | ||
// This is a placeholder; actual implementation depends on ArgType | ||
return 5; | ||
} | ||
|
||
template<typename ClassType, typename ReturnType, typename... Args, size_t... Is> | ||
ReturnType callMethod(ClassType* obj, ReturnType(ClassType::*method)(Args...), jsi::Runtime& runtime, const jsi::Value* args, std::index_sequence<Is...>) { | ||
return (obj->*method)(convertArgument<Args>(runtime, args[Is])...); | ||
} | ||
|
||
|
||
|
||
protected: | ||
template<typename Derived, typename ReturnType, typename... Args> | ||
void registerHybridMethod(std::string name, ReturnType(Derived::*method)(Args...), Derived* derivedInstance) { | ||
auto func = [this, derivedInstance, method](jsi::Runtime &runtime, | ||
const jsi::Value &thisVal, | ||
const jsi::Value *args, | ||
size_t count) -> jsi::Value { | ||
if constexpr (std::is_same_v<ReturnType, void>) { | ||
callMethod(derivedInstance, method, runtime, args, std::index_sequence_for<Args...>{}); | ||
return jsi::Value::undefined(); | ||
} else { | ||
ReturnType result = callMethod(derivedInstance, method, runtime, args, std::index_sequence_for<Args...>{}); | ||
return jsi::Value(result); | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
} // margelo | ||
|
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,8 @@ | ||
// | ||
// Created by Marc Rousavy on 21.02.24. | ||
// | ||
|
||
#include "JsiTypeTraits.h" | ||
|
||
namespace margelo { | ||
} // margelo |
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,49 @@ | ||
// | ||
// Created by Marc Rousavy on 21.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <jsi/jsi.h> | ||
|
||
namespace margelo { | ||
|
||
using namespace facebook; | ||
|
||
// Type conversion templates to and from jsi::Values | ||
template<typename T> | ||
struct JsiTypeTraits { | ||
}; | ||
|
||
template<> | ||
struct JsiTypeTraits<int> { | ||
static int fromJsi(jsi::Runtime& runtime, const jsi::Value& value) { | ||
return value.asNumber(); | ||
} | ||
static jsi::Value toJsi(jsi::Runtime& runtime, int value) { | ||
return jsi::Value(value); | ||
} | ||
}; | ||
|
||
template<> | ||
struct JsiTypeTraits<bool> { | ||
static bool fromJsi(jsi::Runtime& runtime, const jsi::Value& value) { | ||
return value.asNumber(); | ||
} | ||
static jsi::Value toJsi(jsi::Runtime& runtime, bool value) { | ||
return jsi::Value(value); | ||
} | ||
}; | ||
|
||
template<> | ||
struct JsiTypeTraits<std::string> { | ||
static std::string fromJsi(jsi::Runtime& runtime, const jsi::Value& value) { | ||
return value.asString(runtime).utf8(runtime); | ||
} | ||
static jsi::Value toJsi(jsi::Runtime& runtime, std::string value) { | ||
return jsi::String::createFromUtf8(runtime, value); | ||
} | ||
}; | ||
|
||
} // margelo | ||
|