Skip to content

Commit 61fbaf1

Browse files
committed
Create TestHybridObject
1 parent e09d924 commit 61fbaf1

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

package/android/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_library(
2626
../cpp/jsi/HybridObject.cpp
2727
../cpp/jsi/Promise.cpp
2828
../cpp/core/EngineWrapper.cpp
29+
../cpp/test/TestHybridObject.cpp
2930
# Java JNI
3031
src/main/cpp/AndroidFilamentProxy.cpp
3132
src/main/cpp/AndroidSurface.cpp

package/cpp/FilamentProxy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ namespace margelo {
1414

1515
using namespace facebook;
1616

17+
std::shared_ptr<TestHybridObject> FilamentProxy::createTestObject() {
18+
return std::make_shared<TestHybridObject>();
19+
}
20+
1721
void FilamentProxy::loadHybridMethods() {
1822
registerHybridMethod("loadModel", &FilamentProxy::loadModel, this);
1923
registerHybridMethod("findFilamentView", &FilamentProxy::findFilamentView, this);
24+
registerHybridMethod("createTestObject", &FilamentProxy::createTestObject, this);
2025
}
2126

2227
} // namespace margelo

package/cpp/FilamentProxy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "FilamentView.h"
1313
#include "jsi/HybridObject.h"
14+
#include "test/TestHybridObject.h"
1415

1516
namespace margelo {
1617

@@ -21,6 +22,8 @@ class FilamentProxy : public HybridObject {
2122
virtual int loadModel(std::string path) = 0;
2223
virtual std::shared_ptr<FilamentView> findFilamentView(int id) = 0;
2324

25+
std::shared_ptr<TestHybridObject> createTestObject();
26+
2427
public:
2528
void loadHybridMethods() override;
2629
};

package/cpp/jsi/JSIConverter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ struct JSIConverter<std::function<ReturnType(Args...)>> {
121121
size_t count) -> jsi::Value {
122122
callFunction(function, args, std::index_sequence_for<Args...>{});
123123
};
124+
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "hostFunction"), sizeof...(Args), jsFunction);
124125
}
125126
};
126127

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Created by Marc Rousavy on 20.02.24.
3+
//
4+
#include "TestHybridObject.h"
5+
6+
namespace margelo {
7+
8+
void TestHybridObject::loadHybridMethods() {
9+
// this.int get & set
10+
registerHybridGetter("int", &TestHybridObject::getInt, this);
11+
registerHybridSetter("int", &TestHybridObject::setInt, this);
12+
// this.string get & set
13+
registerHybridGetter("string", &TestHybridObject::getString, this);
14+
registerHybridSetter("string", &TestHybridObject::setString, this);
15+
// methods
16+
registerHybridMethod("multipleArguments", &TestHybridObject::multipleArguments, this);
17+
// callbacks
18+
registerHybridMethod("getIntGetter", &TestHybridObject::getIntGetter, this);
19+
registerHybridMethod("sayHelloCallback", &TestHybridObject::sayHelloCallback, this);
20+
// custom types
21+
registerHybridMethod("createNewHybridObject", &TestHybridObject::createNewHybridObject, this);
22+
}
23+
24+
} // namespace margelo
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Created by Marc Rousavy on 22.02.24.
3+
//
4+
5+
#pragma once
6+
7+
#include "jsi/HybridObject.h"
8+
#include <string>
9+
#include <vector>
10+
11+
namespace margelo {
12+
13+
class TestHybridObject : public HybridObject {
14+
public:
15+
int getInt() { return _int; }
16+
void setInt(int newValue) { _int = newValue; }
17+
std::string getString() { return _string; }
18+
void setString(std::string newValue) { _string = newValue; }
19+
20+
std::unordered_map<std::string, double> multipleArguments(int first, bool second, std::string third) {
21+
return std::unordered_map<std::string, double> {
22+
{"first", 5312},
23+
{"second", 532233},
24+
{"third", 2786}
25+
};
26+
}
27+
28+
std::function<int()> getIntGetter() {
29+
return [this]() -> int {
30+
return this->_int;
31+
};
32+
}
33+
void sayHelloCallback(std::function<void(std::string)> callback) {
34+
callback("Test Hybrid");
35+
}
36+
std::shared_ptr<TestHybridObject> createNewHybridObject() {
37+
return std::make_shared<TestHybridObject>();
38+
}
39+
40+
private:
41+
int _int;
42+
std::string _string;
43+
44+
void loadHybridMethods() override;
45+
};
46+
47+
} // namespace margelo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { FilamentProxy } from '../native/FilamentProxy'
2+
3+
interface TestHybridObject {
4+
int: number
5+
string: string
6+
7+
multipleArguments(first: number, second: boolean, third: string): Record<string, number>
8+
getIntGetter(): () => number
9+
sayHelloCallback(callback: () => string): void
10+
createNewHybridObject: () => TestHybridObject
11+
}
12+
13+
const hybridObject = FilamentProxy.createTestObject() as TestHybridObject
14+
console.log(hybridObject.int)

0 commit comments

Comments
 (0)