Skip to content

Commit

Permalink
fix: make working example
Browse files Browse the repository at this point in the history
  • Loading branch information
robik committed Nov 4, 2024
1 parent e27c727 commit 9bf7cba
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
7 changes: 1 addition & 6 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gen/rapier3d.h"
#include <jsi/jsi.h>
#include <ReactCommon/TurboModule.h>
#include "react-native-rapier.h"

using namespace facebook::react;
using namespace facebook;
Expand All @@ -9,12 +10,6 @@ using namespace facebook;
extern "C" {
#endif

struct w2c_wbg {
w2c_rapier__wasm3d__bg* root;
jsi::Runtime& rt;
jsi::Object importObj;
};

/* import: 'wbg' '__wbg_bind_4d857b598695205e' */
u32 w2c_wbg_0x5F_wbg_bind_4d857b598695205e(w2c_wbg* ctx, u32 a, u32 b, u32 c, u32 d) {
auto fn = ctx->importObj.getPropertyAsFunction(ctx->rt, "__wbg_bind_4d857b598695205e");
Expand Down
41 changes: 39 additions & 2 deletions cpp/react-native-rapier.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
#include "react-native-rapier.h"
#include "macros.h"
#include "wasm-rt.h"

namespace facebook::react {

std::shared_ptr<RapierModuleContext> tryGetRapierContext(jsi::Runtime& rt, const jsi::Object& rapierObject) {
if (!rapierObject.hasNativeState(rt)) {
return nullptr;
}

auto context = std::dynamic_pointer_cast<RapierModuleContext>(rapierObject.getNativeState(rt));
return context;
}

ReactNativeRapier::ReactNativeRapier(std::shared_ptr<CallInvoker> jsInvoker)
: NativeReactNativeRapierCxxSpecJSI(jsInvoker), _callInvoker(jsInvoker) {}

double ReactNativeRapier::multiply(jsi::Runtime &rt, double a, double b) {
return a * b;
jsi::Object ReactNativeRapier::create(jsi::Runtime &rt, jsi::Object importObject) {
jsi::Object mod { rt };

if (!wasm_rt_is_initialized()) {
wasm_rt_init();
}

auto inst = std::make_shared<RapierModuleContext>(rt, std::move(importObject));
wasm2c_rapier__wasm3d__bg_instantiate(&inst->ctx, &inst->wbg);

mod.setNativeState(rt, inst);

// Fill Exports
jsi::Object exports {rt};

/* export: 'rawimpulsejointset_new' */
exports.setProperty(rt, "rawimpulsejointset_new", HOSTFN("rawimpulsejointset_new", 0) {
auto nativeState = tryGetRapierContext(rt, thisValue.getObject(rt));
auto res = w2c_rapier__wasm3d__bg_rawimpulsejointset_new(&nativeState->ctx);
return jsi::Value { (double)res };
}));
u32 w2c_rapier__wasm3d__bg_rawimpulsejointset_new(w2c_rapier__wasm3d__bg*);

exports.setNativeState(rt, inst);
mod.setProperty(rt, "exports", std::move(exports));

// end full exports
return mod;
}

}
19 changes: 18 additions & 1 deletion cpp/react-native-rapier.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

#include <ReactCommon/TurboModule.h>
#include <RNReactNativeRapierSpecJSI.h>
#include "gen/rapier3d.h"

struct w2c_wbg {
w2c_rapier__wasm3d__bg* root;
facebook::jsi::Runtime& rt;
facebook::jsi::Object importObj;
};

namespace facebook::react {

using namespace facebook;

class RapierModuleContext: public jsi::NativeState {
public:
RapierModuleContext(jsi::Runtime& rt, jsi::Object&& importObject)
: importObject(std::move(importObject)), wbg(&ctx, rt, this->importObject.getPropertyAsObject(rt, "wbg")) {}

jsi::Object importObject;
w2c_rapier__wasm3d__bg ctx;
w2c_wbg wbg;
};

class ReactNativeRapier : public NativeReactNativeRapierCxxSpecJSI {
public:
explicit ReactNativeRapier(std::shared_ptr<CallInvoker> jsInvoker);

public:
double multiply(jsi::Runtime &rt, double a, double b) override;
jsi::Object create(jsi::Runtime &rt, jsi::Object importObject) override;
constexpr static auto kModuleName = "ReactNativeRapier";

private:
Expand Down
18 changes: 13 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { useState, useEffect } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { useState, useEffect, useCallback } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import Rapier from '@callstack/react-native-rapier';

export default function App() {
const [result, setResult] = useState<number | undefined>();
const [module, setModule] = useState<any | undefined>();

useEffect(() => {
setResult(Rapier.multiply(3, 7));
setModule(Rapier.create({ wbg: {} }));
}, []);

const callFn = useCallback(() => {
console.log(module, module.exports);
let res = module.exports.rawimpulsejointset_new();
console.log('res: ', res);
}, [module]);

return (
<View style={styles.container}>
<Text>Result: {result}</Text>
<Text>Module loaded: {!!module}</Text>
{!!module && <Text>{JSON.stringify(module)}</Text>}
{!!module && <Button title="Call" onPress={callFn} />}
</View>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/NativeReactNativeRapier.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';

export interface Spec extends TurboModule {
multiply(a: number, b: number): number;
create(importObject: UnsafeObject): UnsafeObject;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeRapier');

0 comments on commit 9bf7cba

Please sign in to comment.