Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of explicit resource management #2085

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions example/babel.config.js
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
@@ -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
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
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
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
@@ -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
@@ -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
Expand Up @@ -3,7 +3,8 @@
"target": "esnext",
"lib": [
"dom",
"es2019"
"es2019",
"esnext.disposable"
],
"allowJs": true,
"skipLibCheck": true,
Expand Down