Skip to content

Commit

Permalink
Add support of explicit resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Dec 19, 2023
1 parent 355ff55 commit c5062a5
Show file tree
Hide file tree
Showing 9 changed files with 899 additions and 178 deletions.
1 change: 1 addition & 0 deletions example/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = {
plugins: [
"transform-inline-environment-variables",
"react-native-reanimated/plugin",
"@babel/plugin-proposal-explicit-resource-management",
],
};
4 changes: 4 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/**
* @format
*/
if (!Symbol.dispose) {
Symbol.dispose = Symbol.for("Symbol.dispose");
}

import { AppRegistry } from "react-native";

import App from "./src/App";
Expand Down
7 changes: 4 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
"react-native-web": "^0.18.0"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@babel/core": "^7.23.0",
"@babel/plugin-proposal-explicit-resource-management": "^7.23.3",
"@babel/preset-env": "^7.23.0",
"@babel/runtime": "^7.23.0",
"@react-native-community/eslint-config": "^3.2.0",
"@testing-library/react-native": "12.1.1",
"@tsconfig/react-native": "^2.0.2",
Expand Down
5 changes: 5 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"extends": "eslint-config-react-native-wcandillon/tsconfig.base",
"compilerOptions": {
"noUncheckedIndexedAccess": false,
"lib": [
"dom",
"es2019",
"esnext.disposable"
],
}
}
1,037 changes: 866 additions & 171 deletions example/yarn.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package/cpp/api/JsiSkHostObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <utility>

#include "JsiHostObject.h"
#include "RNSkLog.h"
#include "RNSkPlatformContext.h"

namespace RNSkia {
Expand Down
17 changes: 15 additions & 2 deletions package/cpp/jsi/JsiHostObject.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <JsiHostObject.h>
#include <functional>
#include <vector>

#include <JsiHostObject.h>
#include <RNSkLog.h>

// To be able to find objects that aren't cleaned up correctly,
// we can set this value to 1 and debug the constructor/destructor
#define JSI_DEBUG_ALLOCATIONS 0
Expand Down Expand Up @@ -51,9 +53,20 @@ void JsiHostObject::set(jsi::Runtime &rt, const jsi::PropNameID &name,
}
}

jsi::Value eval(jsi::Runtime &runtime, const std::string &js) {
return runtime.global()
.getPropertyAsFunction(runtime, "eval")
.call(runtime, js);
}

jsi::Value JsiHostObject::get(jsi::Runtime &runtime,
const jsi::PropNameID &name) {
auto nameStr = name.utf8(runtime);
static const auto disposeSymbol = jsi::PropNameID::forSymbol(
runtime,
eval(runtime, "Symbol.for('Symbol.dispose');").getSymbol(runtime));
auto nameStr = jsi::PropNameID::compare(runtime, disposeSymbol, name)
? "dispose"
: name.utf8(runtime);

// Happy path - cached host functions are cheapest to look up
const JsiFunctionMap &funcs = getExportedFunctionMap();
Expand Down
2 changes: 1 addition & 1 deletion package/src/skia/types/JsiInstance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface SkJSIInstance<T extends string> {
export interface SkJSIInstance<T extends string> extends Disposable {
__typename__: T;
dispose(): void;
}
3 changes: 2 additions & 1 deletion package/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "esnext",
"lib": [
"dom",
"es2019"
"es2019",
"esnext.disposable"
],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit c5062a5

Please sign in to comment.