-
Notifications
You must be signed in to change notification settings - Fork 4
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
9 changed files
with
200 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { createCppTestHybridObject, createSwiftTestHybridObject, TestHybridObject } from 'react-native-nitro-modules'; | ||
|
||
const FIBONACCI_N = 33 | ||
const FIBONACCI_COUNT = 5 | ||
|
||
async function runHybridObjectTests(testObject: TestHybridObject): Promise<void> { | ||
try { | ||
console.log('---------- Beginning TestHybridObject Test:') | ||
console.log('Keys:', Object.keys(testObject)) | ||
// Getters & Setters | ||
console.log('get int:', testObject.int) | ||
console.log('set int:', testObject.int = 5) | ||
console.log('get string:', testObject.string) | ||
console.log('set string:', testObject.string = "hello world!") | ||
console.log('get nullable-string:', testObject.nullableString) | ||
console.log('set nullable-string:', testObject.nullableString = "hello world!") | ||
// Methods | ||
console.log('call method:', testObject.multipleArguments(5, false, "hey!")) | ||
// Functions/Lambdas | ||
const getter = testObject.getIntGetter() | ||
console.log('get getter:', getter) | ||
console.log('call getter:', getter()) | ||
testObject.sayHelloCallback((name: string) => { | ||
console.log('call with callback:', name) | ||
}) | ||
|
||
// Promises/Threading | ||
{ | ||
const start = performance.now() | ||
let sum = 0n | ||
for (let i = 0; i < FIBONACCI_COUNT; i++) { | ||
sum += testObject.calculateFibonacci(FIBONACCI_N) | ||
} | ||
const end = performance.now() | ||
console.log(`calculate fibonacci(${FIBONACCI_N}) ${FIBONACCI_COUNT}x sync/serially took ${(end - start).toFixed(2)}ms (result: ${sum})`) | ||
} | ||
{ | ||
const start = performance.now() | ||
const promises: Promise<bigint>[] = [] | ||
for (let i = 0; i < FIBONACCI_COUNT; i++) { | ||
const promise = testObject.calculateFibonacciAsync(FIBONACCI_N) | ||
promises.push(promise) | ||
} | ||
const all = await Promise.all(promises) | ||
const sum = all.reduce((prev, curr) => prev + curr, 0n) | ||
const end = performance.now() | ||
console.log(`calculate fibonacci(${FIBONACCI_N}) ${FIBONACCI_COUNT}x async/parallel took ${(end - start).toFixed(2)}ms (result: ${sum})`) | ||
} | ||
{ | ||
const start = performance.now() | ||
for (let i = 0; i < 100; i++) { | ||
testObject.syncVoidFunc() | ||
} | ||
const end = performance.now() | ||
console.log(`Calling sync func 100 times took ${(end - start).toFixed(2)}ms`) | ||
} | ||
{ | ||
const start = performance.now() | ||
for (let i = 0; i < 100; i++) { | ||
await testObject.asyncVoidFunc() | ||
} | ||
const end = performance.now() | ||
console.log(`Calling async func 100 times took ${(end - start).toFixed(2)}ms`) | ||
} | ||
|
||
// Create another TestHybridObject | ||
const newTest = testObject.createNewHybridObject() | ||
console.log('create new hybrid object:', newTest != testObject) | ||
|
||
// Error throwing | ||
try { | ||
testObject.throwError() | ||
console.error(`throw error didn't throw!!`) | ||
} catch (e: any) { | ||
console.log(`throw error: ${e.message}`) | ||
} | ||
|
||
console.log('---------- Finished TestHybridObject Test!') | ||
} catch (e) { | ||
console.error('---------- Failed TestHybridObject Test!', e) | ||
} | ||
} | ||
|
||
// Tests the C++ object | ||
export async function runCppHybridObjectTests(): Promise<void> { | ||
try { | ||
const cppTestObject = createCppTestHybridObject() | ||
await runHybridObjectTests(cppTestObject) | ||
} catch (e) { | ||
console.error(`Failed to create C++ TestHybridObject!`, e) | ||
} | ||
} | ||
|
||
// Tests the Swift object | ||
export async function runSwiftHybridObjectTests(): Promise<void> { | ||
try { | ||
const swiftTestObject = createSwiftTestHybridObject() | ||
await runHybridObjectTests(swiftTestObject) | ||
} catch (e) { | ||
console.error(`Failed to create Swift TestHybridObject!`, e) | ||
} | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
packages/react-native-nitro-modules/cpp/test-object/SwiftTestHybridObject.cpp
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,28 @@ | ||
// | ||
// SwiftTestHybridObject.cpp | ||
// NitroModules | ||
// | ||
// Created by Marc Rousavy on 23.06.24. | ||
// | ||
|
||
#include "SwiftTestHybridObject.hpp" | ||
|
||
namespace margelo { | ||
|
||
SwiftTestHybridObject::SwiftTestHybridObject(): HybridObject("SwiftTestHybridObject"), _swiftPart(NitroModules::SwiftTestHybridObjectSwift::init()) { | ||
} | ||
|
||
int SwiftTestHybridObject::getInt() { | ||
return _swiftPart.getInt(); | ||
} | ||
|
||
void SwiftTestHybridObject::setInt(int value) { | ||
_swiftPart.setInt(value); | ||
} | ||
|
||
void SwiftTestHybridObject::loadHybridMethods() { | ||
registerHybridGetter("int", &SwiftTestHybridObject::getInt, this); | ||
registerHybridSetter("int", &SwiftTestHybridObject::setInt, this); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
packages/react-native-nitro-modules/cpp/test-object/SwiftTestHybridObject.hpp
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,28 @@ | ||
// | ||
// SwiftTestHybridObject.hpp | ||
// NitroModules | ||
// | ||
// Created by Marc Rousavy on 23.06.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "HybridObject.hpp" | ||
#include "NitroModules-Swift.h" | ||
|
||
namespace margelo { | ||
|
||
class SwiftTestHybridObject: public HybridObject { | ||
public: | ||
explicit SwiftTestHybridObject(); | ||
|
||
int getInt(); | ||
void setInt(int value); | ||
|
||
void loadHybridMethods() override; | ||
|
||
private: | ||
NitroModules::SwiftTestHybridObjectSwift _swiftPart; | ||
}; | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
packages/react-native-nitro-modules/ios/test-object/SwiftTestHybridObject.swift
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,24 @@ | ||
// | ||
// SwiftTestHybridObject.swift | ||
// NitroModules | ||
// | ||
// Created by Marc Rousavy on 23.06.24. | ||
// | ||
|
||
import Foundation | ||
|
||
public class SwiftTestHybridObjectSwift { | ||
private var _int: Int = 5 | ||
public var int: Int { | ||
get { | ||
return _int | ||
} | ||
set { | ||
_int = newValue | ||
} | ||
} | ||
|
||
public init() { | ||
|
||
} | ||
} |
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