Skip to content

Commit 62032ca

Browse files
committed
make v8 work
1 parent 9c8a6fd commit 62032ca

File tree

7 files changed

+3034
-3188
lines changed

7 files changed

+3034
-3188
lines changed

NativeScript/ffi/Block.mm

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
#include "Block.h"
2+
#import <Foundation/Foundation.h>
3+
#include "js_native_api_types.h"
24
#include "Interop.h"
35
#include "ObjCBridge.h"
46
#include "js_native_api.h"
57
#include "node_api_util.h"
68
#include "objc/runtime.h"
7-
#import <Foundation/Foundation.h>
89

910
struct Block_descriptor_1 {
10-
unsigned long int reserved; // NULL
11-
unsigned long int size; // sizeof(struct Block_literal_1)
11+
unsigned long int reserved; // NULL
12+
unsigned long int size; // sizeof(struct Block_literal_1)
1213
// optional helper functions
13-
void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
14-
void (*dispose_helper)(void *src); // IFF (1<<25)
14+
void (*copy_helper)(void* dst, void* src); // IFF (1<<25)
15+
void (*dispose_helper)(void* src); // IFF (1<<25)
1516
// required ABI.2010.3.16
16-
const char *signature; // IFF (1<<30)
17+
const char* signature; // IFF (1<<30)
1718
};
1819

1920
struct Block_literal_1 {
20-
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
21+
void* isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
2122
int flags;
2223
int reserved;
23-
void *invoke;
24-
Block_descriptor_1 *descriptor;
24+
void* invoke;
25+
Block_descriptor_1* descriptor;
2526
// imported variables
26-
objc_bridge::Closure *closure;
27+
objc_bridge::Closure* closure;
2728
};
2829

29-
void block_copy(void *dest, void *src) {}
30-
void block_release(void *src) {}
30+
void block_copy(void* dest, void* src) {}
31+
void block_release(void* src) {}
3132

32-
void block_finalize(napi_env env, void *data, void *hint) {
33-
auto block = (Block_literal_1 *)data;
33+
void block_finalize(napi_env env, void* data, void* hint) {
34+
auto block = (Block_literal_1*)data;
3435
delete block->closure;
3536
delete block;
3637
}
3738

3839
namespace objc_bridge {
3940

40-
void *stackBlockISA = nullptr;
41+
void* stackBlockISA = nullptr;
4142

42-
id registerBlock(napi_env env, Closure *closure, napi_value callback) {
43+
id registerBlock(napi_env env, Closure* closure, napi_value callback) {
4344
auto block = new Block_literal_1();
4445
if (stackBlockISA == nullptr) {
4546
stackBlockISA = dlsym(RTLD_DEFAULT, "_NSConcreteStackBlock");
@@ -59,22 +60,24 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
5960

6061
napi_remove_wrap(env, callback, nullptr);
6162
napi_ref ref = nullptr;
62-
napi_wrap(env, callback, block, block_finalize, nullptr, &ref);
63-
if (ref == nullptr) {
63+
// TODO: fix memory management of objc blocks here
64+
// napi_wrap(env, callback, block, block_finalize, nullptr, &ref);
65+
// if (ref == nullptr) {
6466
// Deno doesn't handle napi_wrap properly.
65-
ref = make_ref(env, callback, 0);
66-
}
67+
ref = make_ref(env, callback, 1);
68+
// } else {
69+
// uint32_t refCount;
70+
// napi_reference_ref(env, ref, &refCount);
71+
// }
6772
closure->func = ref;
6873

6974
auto bridgeState = ObjCBridgeState::InstanceData(env);
7075
if (napiSupportsThreadsafeFunctions(bridgeState->self_dl)) {
7176
napi_value workName;
7277
napi_create_string_utf8(env, "Block", NAPI_AUTO_LENGTH, &workName);
73-
napi_create_threadsafe_function(
74-
env, callback, nullptr, workName, 0, 1, nullptr, nullptr, closure,
75-
Closure::callBlockFromMainThread, &closure->tsfn);
76-
if (closure->tsfn)
77-
napi_unref_threadsafe_function(env, closure->tsfn);
78+
napi_create_threadsafe_function(env, callback, nullptr, workName, 0, 1, nullptr, nullptr,
79+
closure, Closure::callBlockFromMainThread, &closure->tsfn);
80+
if (closure->tsfn) napi_unref_threadsafe_function(env, closure->tsfn);
7881
}
7982

8083
return (id)block;
@@ -98,14 +101,13 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
98101
return callback;
99102
}
100103

101-
napi_value FunctionPointer::wrap(napi_env env, void *function,
102-
metagen::MDSectionOffset offset,
104+
napi_value FunctionPointer::wrap(napi_env env, void* function, metagen::MDSectionOffset offset,
103105
bool isBlock) {
104-
FunctionPointer *ref = new FunctionPointer();
106+
FunctionPointer* ref = new FunctionPointer();
105107
ref->function = function;
106108
ref->offset = offset;
107109

108-
ObjCBridgeState *bridgeState = ObjCBridgeState::InstanceData(env);
110+
ObjCBridgeState* bridgeState = ObjCBridgeState::InstanceData(env);
109111

110112
if (isBlock) {
111113
ref->cif = bridgeState->getBlockCif(env, offset);
@@ -114,36 +116,32 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
114116
}
115117

116118
napi_value result;
117-
napi_create_function(
118-
env, isBlock ? "objcBlockWrapper" : "cFunctionWrapper", NAPI_AUTO_LENGTH,
119-
isBlock ? jsCallAsBlock : jsCallAsCFunction, ref, &result);
119+
napi_create_function(env, isBlock ? "objcBlockWrapper" : "cFunctionWrapper", NAPI_AUTO_LENGTH,
120+
isBlock ? jsCallAsBlock : jsCallAsCFunction, ref, &result);
120121

121122
napi_ref jsRef;
122-
napi_add_finalizer(env, result, ref, FunctionPointer::finalize, nullptr,
123-
&jsRef);
123+
napi_add_finalizer(env, result, ref, FunctionPointer::finalize, nullptr, &jsRef);
124124

125125
return result;
126126
}
127127

128-
void FunctionPointer::finalize(napi_env env, void *finalize_data,
129-
void *finalize_hint) {
130-
auto ref = (FunctionPointer *)finalize_data;
128+
void FunctionPointer::finalize(napi_env env, void* finalize_data, void* finalize_hint) {
129+
auto ref = (FunctionPointer*)finalize_data;
131130
delete ref;
132131
}
133132

134-
napi_value FunctionPointer::jsCallAsCFunction(napi_env env,
135-
napi_callback_info cbinfo) {
136-
FunctionPointer *ref;
133+
napi_value FunctionPointer::jsCallAsCFunction(napi_env env, napi_callback_info cbinfo) {
134+
FunctionPointer* ref;
137135

138-
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void **)&ref);
136+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&ref);
139137

140138
auto cif = ref->cif;
141139

142140
size_t argc = cif->argc;
143141
napi_get_cb_info(env, cbinfo, &argc, cif->argv, nullptr, nullptr);
144142

145-
void *avalues[cif->argc];
146-
void *rvalue = cif->rvalue;
143+
void* avalues[cif->argc];
144+
void* rvalue = cif->rvalue;
147145

148146
bool shouldFreeAny = false;
149147
bool shouldFree[cif->argc];
@@ -152,8 +150,7 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
152150
for (unsigned int i = 0; i < cif->argc; i++) {
153151
shouldFree[i] = false;
154152
avalues[i] = cif->avalues[i];
155-
cif->argTypes[i]->toNative(env, cif->argv[i], avalues[i], &shouldFree[i],
156-
&shouldFreeAny);
153+
cif->argTypes[i]->toNative(env, cif->argv[i], avalues[i], &shouldFree[i], &shouldFreeAny);
157154
}
158155
}
159156

@@ -162,28 +159,27 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
162159
if (shouldFreeAny) {
163160
for (unsigned int i = 0; i < cif->argc; i++) {
164161
if (shouldFree[i]) {
165-
cif->argTypes[i]->free(env, *((void **)avalues[i]));
162+
cif->argTypes[i]->free(env, *((void**)avalues[i]));
166163
}
167164
}
168165
}
169166

170167
return cif->returnType->toJS(env, rvalue);
171168
}
172169

173-
napi_value FunctionPointer::jsCallAsBlock(napi_env env,
174-
napi_callback_info cbinfo) {
175-
FunctionPointer *ref;
170+
napi_value FunctionPointer::jsCallAsBlock(napi_env env, napi_callback_info cbinfo) {
171+
FunctionPointer* ref;
176172

177-
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void **)&ref);
173+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, (void**)&ref);
178174

179-
Block_literal_1 *block = (Block_literal_1 *)ref->function;
175+
Block_literal_1* block = (Block_literal_1*)ref->function;
180176
auto cif = ref->cif;
181177

182178
size_t argc = cif->argc;
183179
napi_get_cb_info(env, cbinfo, &argc, cif->argv, nullptr, nullptr);
184180

185-
void *avalues[cif->cif.nargs];
186-
void *rvalue = cif->rvalue;
181+
void* avalues[cif->cif.nargs];
182+
void* rvalue = cif->rvalue;
187183

188184
bool shouldFreeAny = false;
189185
bool shouldFree[cif->argc];
@@ -194,8 +190,7 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
194190
for (unsigned int i = 0; i < cif->argc; i++) {
195191
shouldFree[i] = false;
196192
avalues[i + 1] = cif->avalues[i];
197-
cif->argTypes[i]->toNative(env, cif->argv[i], avalues[i + 1],
198-
&shouldFree[i], &shouldFreeAny);
193+
cif->argTypes[i]->toNative(env, cif->argv[i], avalues[i + 1], &shouldFree[i], &shouldFreeAny);
199194
}
200195
}
201196

@@ -204,12 +199,12 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
204199
if (shouldFreeAny) {
205200
for (unsigned int i = 0; i < cif->argc; i++) {
206201
if (shouldFree[i]) {
207-
cif->argTypes[i]->free(env, *((void **)avalues[i + 1]));
202+
cif->argTypes[i]->free(env, *((void**)avalues[i + 1]));
208203
}
209204
}
210205
}
211206

212207
return cif->returnType->toJS(env, rvalue);
213208
}
214209

215-
} // namespace objc_bridge
210+
} // namespace objc_bridge

NativeScript/ffi/TypeConv.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,6 @@ void toNative(napi_env env, napi_value value, void *result, bool *shouldFree,
689689
auto bridgeState = ObjCBridgeState::InstanceData(env);
690690
auto closure = new Closure(bridgeState->metadata, signatureOffset, true);
691691
closure->env = env;
692-
closure->func = make_ref(env, value);
693692
id block = registerBlock(env, closure, value);
694693
*res = (void *)block;
695694
return;

NativeScript/napi/v8/jsr.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#include "jsr.h"
2-
#include "File.h"
32
#include <libgen.h>
43
#include <dlfcn.h>
54
#include <sys/stat.h>
65
#include <ctime>
76
#include <utime.h>
87
#include "v8-fast-api-calls.h"
9-
#include "NativeScriptAssert.h"
108

119
using namespace v8;
1210
using namespace tns;
@@ -28,7 +26,7 @@ static void divide(const FunctionCallbackInfo<Value>& args) {
2826
}
2927

3028
static double FastDivide(const int32_t this_arg, const int32_t a, const int32_t b, v8::FastApiCallbackOptions& options) {
31-
DEBUG_WRITE_FORCE("%s", "FAST");
29+
// DEBUG_WRITE_FORCE("%s", "FAST");
3230
if (b == 0) {
3331
options.fallback = true;
3432
return 0;
@@ -206,7 +204,10 @@ napi_status js_cache_script(napi_env env, const char *source, const char *file)
206204

207205
int length = cachedData->length;
208206
auto cachePath = std::string(file) + ".cache";
209-
File::WriteBinary(cachePath, cachedData->data, length);
207+
// File::WriteBinary(cachePath, cachedData->data, length);
208+
std::ofstream cacheFile(cachePath, std::ios::binary);
209+
cacheFile.write(reinterpret_cast<const char*>(cachedData->data), length);
210+
cacheFile.close();
210211
delete cachedData;
211212
// make sure cache and js file have the same modification date
212213
struct stat result;
@@ -238,10 +239,23 @@ napi_status js_run_cached_script(napi_env env, const char * file, napi_value scr
238239
}
239240

240241
int length = 0;
241-
auto data = File::ReadBinary(cachePath, length);
242-
if (!data) {
242+
243+
// auto data = File::ReadBinary(cachePath, length);
244+
// if (!data) {
245+
// return napi_cannot_run_js;
246+
// }
247+
248+
std::ifstream cacheFile(cachePath, std::ios::binary);
249+
if (!cacheFile.is_open()) {
243250
return napi_cannot_run_js;
244251
}
252+
cacheFile.seekg(0, std::ios::end);
253+
length = cacheFile.tellg();
254+
cacheFile.seekg(0, std::ios::beg);
255+
char* data = new char[length];
256+
cacheFile.read(data, length);
257+
cacheFile.close();
258+
245259

246260
auto * cacheData = new ScriptCompiler::CachedData(reinterpret_cast<uint8_t*>(data), length, ScriptCompiler::CachedData::BufferOwned);
247261
std::string filePath = std::string("file://") + file;

NativeScript/napi/v8/jsr.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "jsr_common.h"
1010
#include "libplatform/libplatform.h"
1111
#include "SimpleAllocator.h"
12-
#include "JEnv.h"
12+
// #include "JEnv.h"
1313

1414
typedef struct napi_runtime__ *napi_runtime;
1515

@@ -92,9 +92,9 @@ v8::HandleScope handle_scope(env->isolate);
9292

9393
namespace tns {
9494

95-
inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate, const jchar* data, int length) {
96-
return v8::String::NewFromTwoByte(isolate, (const uint16_t*) data, v8::NewStringType::kNormal, length).ToLocalChecked();
97-
}
95+
// inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate, const jchar* data, int length) {
96+
// return v8::String::NewFromTwoByte(isolate, (const uint16_t*) data, v8::NewStringType::kNormal, length).ToLocalChecked();
97+
// }
9898

9999
inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate, const std::string& s) {
100100
return v8::String::NewFromUtf8(isolate, s.c_str(), v8::NewStringType::kNormal, s.length()).ToLocalChecked();
@@ -124,19 +124,19 @@ namespace tns {
124124
}
125125

126126

127-
static v8::Local<v8::Value> jstringToV8String(v8::Isolate* isolate, jstring value) {
128-
if (value == nullptr) {
129-
return Null(isolate);
130-
}
127+
// static v8::Local<v8::Value> jstringToV8String(v8::Isolate* isolate, jstring value) {
128+
// if (value == nullptr) {
129+
// return Null(isolate);
130+
// }
131131

132-
JEnv env;
133-
auto chars = env.GetStringChars(value, NULL);
134-
auto length = env.GetStringLength(value);
135-
auto v8String = tns::ConvertToV8String(isolate, chars, length);
136-
env.ReleaseStringChars(value, chars);
132+
// JEnv env;
133+
// auto chars = env.GetStringChars(value, NULL);
134+
// auto length = env.GetStringLength(value);
135+
// auto v8String = tns::ConvertToV8String(isolate, chars, length);
136+
// env.ReleaseStringChars(value, chars);
137137

138-
return v8String;
139-
}
138+
// return v8String;
139+
// }
140140

141141
inline static std::string ToString(v8::Isolate *isolate, const v8::Local<v8::Value> &value) {
142142
if (value.IsEmpty()) {
@@ -158,20 +158,20 @@ namespace tns {
158158
return std::string(*result, result.length());
159159
}
160160

161-
static std::string jstringToString(jstring value) {
162-
if (value == nullptr) {
163-
return {};
164-
}
161+
// static std::string jstringToString(jstring value) {
162+
// if (value == nullptr) {
163+
// return {};
164+
// }
165165

166-
JEnv env;
166+
// JEnv env;
167167

168-
jboolean f = JNI_FALSE;
169-
auto chars = env.GetStringUTFChars(value, &f);
170-
std::string s(chars);
171-
env.ReleaseStringUTFChars(value, chars);
168+
// jboolean f = JNI_FALSE;
169+
// auto chars = env.GetStringUTFChars(value, &f);
170+
// std::string s(chars);
171+
// env.ReleaseStringUTFChars(value, chars);
172172

173-
return s;
174-
}
173+
// return s;
174+
// }
175175
}
176176

177177
#endif //TEST_APP_JSR_H

0 commit comments

Comments
 (0)