ccgus / CocoaScript

JavaScript + the Cocoa frameworks, and then ObjC brackets show up to party as well.
Other
618 stars 58 forks source link

Running mutiple COScript and using background threads, anyway serialise them #59

Open UXDart opened 6 years ago

UXDart commented 6 years ago

Hello! I'm writing a plugin for Sketch, and inside the cocoa script I use performSelectorInBackground_withObject to start a new thread and don't block the execution of a log running process. that works perfect! I call coscript.setShouldKeepAround(true); and coscript.setShouldKeepAround(false); when the thread ends. that works ok.

the problem is that, if I start (lets say) 3 scripts, different names, different COScript instances, this happens:

so for some reason it seems something is making them stop/wait when other thread is working. I know COScript does not really support threads, but they work... in some way... the question is, why this could be happening? I tried many things, but in all cases this happens. if someone has any tip I can work over that, but I just don't know what else to try... thx!

TIA!

UXDart commented 6 years ago

hmm.. they are sharing the same JSVirtualMachine... so that makes sense then... that must be the reason right? where is COScript creating the JSVirtualMachine? or how can I create a new one for each COScript instance?

Thanks

UXDart commented 6 years ago

found a way to create them with different JSVirtualMachine, so each one has its own Mocha instance, and it happens anyway, threads gets serialised... any tip? TIA!

ccgus commented 6 years ago

Are you creating the CocoaScript instances, or is this something that Sketch is handing off to you?

UXDart commented 6 years ago

I tried with Sketch ones, didn't work. so I'm creating the instances myself, so I can do whatever is needed to make it work. atm I find a way to create each instance of Mocha inside COScript using a different JSVirtualMachine, using initWithGlobalContext method. but then nothing works, it seems it can't set a variable there for some reason. I think that is the issue, that Sketch is doing a Mocha.alloc.init so it using the same Mocha global context. it makes sense?

do you have a sample of how to use the method initWithGlobalContext? what do I need to send exactly so it has a different global context?

or what do you think may be the cause of the serialising of the threads?

Thanks!

UXDart commented 6 years ago

I'm doing this atm:

JSContext *contextNew = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]]; JSGlobalContextRef globalCtx1 = contextNew.JSGlobalContextRef; _mochaRuntime = [[Mocha alloc] initWithGlobalContext:globalCtx1 name:nil];

but that seems to work, but then the setvalue getvalue doesn't work...

ccgus commented 6 years ago

I'm not sure what would be causing it off the top of my head. You might ping the sketch folks.