-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathJSIExampleHostObject.cpp
51 lines (44 loc) · 1.67 KB
/
JSIExampleHostObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "JSIExampleHostObject.h"
#include <jsi/jsi.h>
namespace example {
using namespace facebook;
std::vector<jsi::PropNameID> JSIExampleHostObject::getPropertyNames(jsi::Runtime &runtime)
{
std::vector<jsi::PropNameID> propertyNames;
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "multiply"));
return propertyNames;
}
jsi::Value JSIExampleHostObject::get(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) {
auto propName = propNameId.utf8(runtime);
if (propName == "multiply") {
return jsi::Function::createFromHostFunction(runtime, propNameId, 2,
[this](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) {
if (count != 2) {
throw std::invalid_argument("multiply expects exactly two arguments");
}
return this->multiply(rt, args[0], args[1]);
});
}
throw std::runtime_error("Not yet implemented!");
}
void JSIExampleHostObject::set(jsi::Runtime &runtime, const jsi::PropNameID &propNameId, const jsi::Value &value)
{
auto propName = propNameId.utf8(runtime);
if (propName == "multiply")
{
// Do nothing
return;
}
throw std::runtime_error("Not yet implemented!");
}
jsi::Value JSIExampleHostObject::multiply(jsi::Runtime &runtime, const jsi::Value &value, const jsi::Value &value2) {
if (value.isNumber() && value2.isNumber()) {
// Extract numbers and add them
double result = value.asNumber() + value2.asNumber();
return jsi::Value(result);
} else {
// Handle other cases (e.g., one is a number and the other is a string)
return jsi::Value::undefined();
}
}
}