Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build/project-template/__PROJECT_NAME__/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ int main(int argc, char *argv[]) {
#endif
[TNSRuntime initializeMetadata:&startOfMetadataSection];
runtime = [[TNSRuntime alloc] initWithApplicationPath:applicationPath];
[runtime scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];

#ifndef NDEBUG
[TNSRuntimeInspector setLogsToSystemConsole:YES];
Expand Down
2 changes: 2 additions & 0 deletions cmake/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ int main(int argc, char *argv[]) {
[TNSRuntime initializeMetadata:&startOfMetadataSection];
runtime = [[TNSRuntime alloc]
initWithApplicationPath:[NSBundle mainBundle].bundlePath];
[runtime scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
TNSRuntimeInspector.logsToSystemConsole = YES;

#ifndef NDEBUG
Expand Down
2 changes: 2 additions & 0 deletions examples/BlankApp/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ int main(int argc, char *argv[]) {
[TNSRuntime initializeMetadata:&startOfMetadataSection];
TNSRuntime *runtime = [[TNSRuntime alloc]
initWithApplicationPath:[NSBundle mainBundle].bundlePath];
[runtime scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
TNSRuntimeInspector.logsToSystemConsole = YES;

NSError *error = nil;
Expand Down
8 changes: 8 additions & 0 deletions src/NativeScript/GlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class GlobalObject : public JSC::JSGlobalObject {
return this->_fastEnumerationIteratorStructure.get();
}

void setMicrotaskRunLoopAndMode(CFRunLoopRef runLoop, CFTypeRef mode) {
this->_microtaskRunLoop = runLoop;
this->_microtaskRunLoopMode = mode;
}

#if defined(__LP64__) && __LP64__
JSC::WeakGCMap<const void*, ObjCWrapperObject>& taggedPointers() {
return this->_taggedPointers;
Expand All @@ -145,6 +150,9 @@ class GlobalObject : public JSC::JSGlobalObject {

WTF::String _applicationPath;

WTF::RetainPtr<CFRunLoopRef> _microtaskRunLoop;
WTF::RetainPtr<CFTypeRef> _microtaskRunLoopMode;

JSC::WriteBarrier<JSC::Structure> _objCMethodCallStructure;
JSC::WriteBarrier<JSC::Structure> _objCConstructorCallStructure;
JSC::WriteBarrier<JSC::Structure> _objCBlockCallStructure;
Expand Down
7 changes: 6 additions & 1 deletion src/NativeScript/GlobalObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,14 @@ static EncodedJSValue JSC_HOST_CALL collectGarbage(ExecState* execState) {
}

void GlobalObject::queueTaskToEventLoop(const JSGlobalObject* globalObject, WTF::PassRefPtr<Microtask> task) {
CFRunLoopPerformBlock(CFRunLoopGetCurrent(), kCFRunLoopCommonModes, ^{
auto global = jsCast<const GlobalObject*>(globalObject);
CFRunLoopRef runLoop = global->_microtaskRunLoop.get() ?: CFRunLoopGetCurrent();
CFTypeRef mode = global->_microtaskRunLoopMode.get() ?: kCFRunLoopCommonModes;

CFRunLoopPerformBlock(runLoop, mode, ^{
JSLockHolder lock(globalObject->vm());
task->run(const_cast<JSGlobalObject*>(globalObject)->globalExec());
});
CFRunLoopWakeUp(runLoop);
}
}
4 changes: 4 additions & 0 deletions src/NativeScript/TNSRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ FOUNDATION_EXTERN void TNSSetUncaughtErrorHandler(TNSUncaughtErrorHandler handle

- (instancetype)initWithApplicationPath:(NSString*)applicationPath;

- (void)scheduleInRunLoop:(NSRunLoop*)runLoop forMode:(NSString*)mode;

- (void)removeFromRunLoop:(NSRunLoop*)runLoop forMode:(NSString*)mode;

- (JSGlobalContextRef)globalContext;

- (void)executeModule:(NSString*)entryPointModuleIdentifier;
Expand Down
8 changes: 8 additions & 0 deletions src/NativeScript/TNSRuntime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ - (instancetype)initWithApplicationPath:(NSString*)applicationPath {
return self;
}

- (void)scheduleInRunLoop:(NSRunLoop*)runLoop forMode:(NSString*)mode {
self->_globalObject->setMicrotaskRunLoopAndMode([runLoop getCFRunLoop], mode);
}

- (void)removeFromRunLoop:(NSRunLoop*)runLoop forMode:(NSString*)mode {
self->_globalObject->setMicrotaskRunLoopAndMode(nullptr, nullptr);
}

- (JSGlobalContextRef)globalContext {
return toGlobalRef(self->_globalObject->globalExec());
}
Expand Down
5 changes: 5 additions & 0 deletions tests/TestRunner/app/Promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("Promise scheduling", function () {
it("should be executed", function(done) {
Promise.resolve().then(done);
});
});
2 changes: 2 additions & 0 deletions tests/TestRunner/app/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ require('./shared');
require('./ApiTests');
require('./DeclarationConflicts');

require('./Promises');

execute();

UIApplicationMain(0, null, null, null);