summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxant <xant@xant.net>2013-07-12 10:39:23 (GMT)
committer xant <xant@xant.net>2013-07-12 10:39:23 (GMT)
commitb6aacf9b01d56d0867d4632b38290a27b6cf9150 (patch)
tree714bcdcc5f48e993ef52b4029bd6e4b0779e4929
parent1d73174ff02db0e54d24df2deefce4c71337366e (diff)
re-enabled some locks since timers are now executed from a different thread
deprecated the old Run() based api ... in favour of the addToRunLoop and removeFromRunLoop one timers and intervals can also be used (which are pretty standard)
-rw-r--r--core/JMXScript.mm126
-rw-r--r--core/JMXScriptTimer.h3
-rw-r--r--core/JMXScriptTimer.mm5
-rw-r--r--core/NSXMLNode+V8.mm78
-rw-r--r--javascript_examples/draw_pixels.js3
5 files changed, 114 insertions, 101 deletions
diff --git a/core/JMXScript.mm b/core/JMXScript.mm
index d108a1f..821a047 100644
--- a/core/JMXScript.mm
+++ b/core/JMXScript.mm
@@ -371,65 +371,65 @@ static v8::Handle<Value> Sleep(const Arguments& args)
return Undefined();
}
-static v8::Handle<Value> Run(const Arguments& args)
-{
- HandleScope handleScope;
- v8::Locker locker;
- Local<Context> context = v8::Context::GetCalling();
- Local<Object> globalObject = context->Global();
- //JMXEntity *entity = (JMXEntity *)globalObject->GetPointerFromInternalField(0);
- //v8::Locker::StopPreemption();
- //globalObject->SetHiddenValue(String::New("quit"), v8::Boolean::New(0));
- if (args.Length() >= 1 && args[0]->IsFunction()) {
- v8::Locker::StartPreemption(50);
- while (1) {
- HandleScope iterationScope;
- if (globalObject->GetHiddenValue(String::New("quit"))->BooleanValue())
- break;
-
- Local<Value> global = globalObject->Get(String::New("scriptEntity"));
- if (global.IsEmpty() || global->IsUndefined() || global->IsNull())
- break;
- v8::Local<v8::Object> obj = global->ToObject();
-
- JMXScriptEntity *entity = (JMXScriptEntity *)obj->GetPointerFromInternalField(0);
- //ctx->Global()->Set(String::New("scriptEntity"), [scriptEntity jsObj]);
-
- if (!entity.active) {
- break;
- }
- v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(args[0]);
- int nArgs = args.Length() ? args.Length() - 1 : 0;
- v8::Handle<v8::Value> *fArgs = nArgs
- ? (v8::Handle<v8::Value> *)malloc(sizeof(v8::Handle<v8::Value>) * nArgs)
- : nil;
- for (int i = 0; i < nArgs; i++)
- fArgs[i] = args[i+1];
- //v8::Local<v8::Value> result =
- foo->Call(foo, nArgs, fArgs);
- if (fArgs)
- free(fArgs);
- //uv_run(uv_default_loop(), (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT));
- }
- // restore quit status for nested loops
- v8::Locker::StopPreemption();
- globalObject->SetHiddenValue(String::New("quit"), v8::Boolean::New(0));
- }
- //v8::Locker::StartPreemption(50);
- NSLog(@"Javascript run() is exiting");
- v8::Handle<Primitive> t = Undefined();
- return reinterpret_cast<v8::Handle<String>&>(t);
-}
-
-static v8::Handle<Value> Quit(const Arguments& args)
-{
- //v8::Locker lock;
- HandleScope handleScope;
- Local<Context> globalContext = v8::Context::GetCurrent();
- Local<Object> globalObject = globalContext->Global();
- globalObject->SetHiddenValue(String::New("quit"), v8::Boolean::New(1));
- return Undefined();
-}
+//static v8::Handle<Value> Run(const Arguments& args)
+//{
+// HandleScope handleScope;
+// v8::Locker locker;
+// Local<Context> context = v8::Context::GetCalling();
+// Local<Object> globalObject = context->Global();
+// //JMXEntity *entity = (JMXEntity *)globalObject->GetPointerFromInternalField(0);
+// //v8::Locker::StopPreemption();
+// //globalObject->SetHiddenValue(String::New("quit"), v8::Boolean::New(0));
+// if (args.Length() >= 1 && args[0]->IsFunction()) {
+// v8::Locker::StartPreemption(50);
+// while (1) {
+// HandleScope iterationScope;
+// if (globalObject->GetHiddenValue(String::New("quit"))->BooleanValue())
+// break;
+//
+// Local<Value> global = globalObject->Get(String::New("scriptEntity"));
+// if (global.IsEmpty() || global->IsUndefined() || global->IsNull())
+// break;
+// v8::Local<v8::Object> obj = global->ToObject();
+//
+// JMXScriptEntity *entity = (JMXScriptEntity *)obj->GetPointerFromInternalField(0);
+// //ctx->Global()->Set(String::New("scriptEntity"), [scriptEntity jsObj]);
+//
+// if (!entity.active) {
+// break;
+// }
+// v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(args[0]);
+// int nArgs = args.Length() ? args.Length() - 1 : 0;
+// v8::Handle<v8::Value> *fArgs = nArgs
+// ? (v8::Handle<v8::Value> *)malloc(sizeof(v8::Handle<v8::Value>) * nArgs)
+// : nil;
+// for (int i = 0; i < nArgs; i++)
+// fArgs[i] = args[i+1];
+// //v8::Local<v8::Value> result =
+// foo->Call(foo, nArgs, fArgs);
+// if (fArgs)
+// free(fArgs);
+// //uv_run(uv_default_loop(), (uv_run_mode)(UV_RUN_ONCE | UV_RUN_NOWAIT));
+// }
+// // restore quit status for nested loops
+// v8::Locker::StopPreemption();
+// globalObject->SetHiddenValue(String::New("quit"), v8::Boolean::New(0));
+// }
+// //v8::Locker::StartPreemption(50);
+// NSLog(@"Javascript run() is exiting");
+// v8::Handle<Primitive> t = Undefined();
+// return reinterpret_cast<v8::Handle<String>&>(t);
+//}
+
+//static v8::Handle<Value> Quit(const Arguments& args)
+//{
+// //v8::Locker lock;
+// HandleScope handleScope;
+// Local<Context> globalContext = v8::Context::GetCurrent();
+// Local<Object> globalObject = globalContext->Global();
+// globalObject->SetHiddenValue(String::New("quit"), v8::Boolean::New(1));
+// return Undefined();
+//}
static v8::Handle<Value> GetDocument(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
@@ -749,8 +749,8 @@ static char *argv[2] = { (char *)"JMX", NULL };
ctxTemplate->Set(String::New("isdir"), FunctionTemplate::New(IsDir));
ctxTemplate->Set(String::New("exportPin"), FunctionTemplate::New(ExportPin));
ctxTemplate->Set(String::New("dumpDOM"), FunctionTemplate::New(DumpDOM));
- ctxTemplate->Set(String::New("run"), FunctionTemplate::New(Run));
- ctxTemplate->Set(String::New("quit"), FunctionTemplate::New(Quit));
+// ctxTemplate->Set(String::New("run"), FunctionTemplate::New(Run));
+// ctxTemplate->Set(String::New("quit"), FunctionTemplate::New(Quit));
ctxTemplate->Set(String::New("addToRunLoop"), FunctionTemplate::New(AddToRunLoop));
ctxTemplate->Set(String::New("removeFromRunLoop"), FunctionTemplate::New(RemoveFromRunLoop));
#if 0
@@ -828,6 +828,10 @@ static char *argv[2] = { (char *)"JMX", NULL };
- (void)clearTimers
{
+ for (JMXScriptTimer *t in runloopTimers) {
+ [t invalidate];
+ }
+ [runloopTimers removeAllObjects];
[self execCode:@"clearAllTimers()"];
}
diff --git a/core/JMXScriptTimer.h b/core/JMXScriptTimer.h
index 3e1c36c..050dcb3 100644
--- a/core/JMXScriptTimer.h
+++ b/core/JMXScriptTimer.h
@@ -40,4 +40,7 @@
target:(id)target
selector:(SEL)selector
repeats:(BOOL)repeats;
+
+- (void)invalidate;
+
@end
diff --git a/core/JMXScriptTimer.mm b/core/JMXScriptTimer.mm
index 65d2251..8c52638 100644
--- a/core/JMXScriptTimer.mm
+++ b/core/JMXScriptTimer.mm
@@ -72,6 +72,11 @@ static v8::Persistent<FunctionTemplate> objectTemplate;
return objectTemplate;
}
+- (void)invalidate
+{
+ [timer invalidate];
+}
+
- (void)dealloc
{
if (!function.IsEmpty())
diff --git a/core/NSXMLNode+V8.mm b/core/NSXMLNode+V8.mm
index ccc6be0..1554b03 100644
--- a/core/NSXMLNode+V8.mm
+++ b/core/NSXMLNode+V8.mm
@@ -42,7 +42,7 @@ using namespace v8;
static v8::Handle<Value>GetParentNode(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
Local<Value> ret;
@@ -58,7 +58,7 @@ static v8::Handle<Value>GetParentNode(Local<String> name, const AccessorInfo& in
static v8::Handle<Value>GetChildNodes(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@@ -85,15 +85,17 @@ static v8::Handle<Value>GetChildNodes(Local<String> name, const AccessorInfo& in
static v8::Handle<Value>GetFirstChild(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
- return handleScope.Close([[node childAtIndex:0] jsObj]);
+ if ([node childCount] > 0)
+ return handleScope.Close([[node childAtIndex:0] jsObj]);
+ return handleScope.Close(Undefined());
}
static v8::Handle<Value>GetLastChild(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close([[node childAtIndex:[node childCount]-1] jsObj]);
@@ -101,7 +103,7 @@ static v8::Handle<Value>GetLastChild(Local<String> name, const AccessorInfo& inf
static v8::Handle<Value>GetPreviousSibling(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
NSXMLNode *sibling = [node previousSibling];
@@ -113,7 +115,7 @@ static v8::Handle<Value>GetPreviousSibling(Local<String> name, const AccessorInf
static v8::Handle<Value>GetNextSibling(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
NSXMLNode *sibling = [node nextSibling];
@@ -125,7 +127,7 @@ static v8::Handle<Value>GetNextSibling(Local<String> name, const AccessorInfo& i
static v8::Handle<Value>GetAttributes(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
Local<Context> ctx = v8::Context::GetCurrent();
Local<Function> constructor = v8::Local<v8::Function>::Cast(ctx->Global()->Get(String::New("NamedNodeMap")));
@@ -152,7 +154,7 @@ static v8::Handle<Value>GetAttributes(Local<String> name, const AccessorInfo& in
static v8::Handle<Value>GetNameSpaceURI(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close(String::New([[node URI] UTF8String]));
@@ -160,15 +162,15 @@ static v8::Handle<Value>GetNameSpaceURI(Local<String> name, const AccessorInfo&
static v8::Handle<Value>GetLocalName(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close(String::New([[node localName] UTF8String]));
}
static v8::Handle<Value>GetOwnerDocument(Local<String> name, const AccessorInfo& info)
-{
- //v8::Locker lock;
+{
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close([[node rootDocument] jsObj]);
@@ -176,7 +178,7 @@ static v8::Handle<Value>GetOwnerDocument(Local<String> name, const AccessorInfo&
static v8::Handle<Value>GetPrefix(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close(String::New([[node prefix] UTF8String]));
@@ -184,7 +186,7 @@ static v8::Handle<Value>GetPrefix(Local<String> name, const AccessorInfo& info)
static v8::Handle<Value>GetBaseURI(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close(String::New([[[node rootDocument] URI] UTF8String]));
@@ -192,7 +194,7 @@ static v8::Handle<Value>GetBaseURI(Local<String> name, const AccessorInfo& info)
static v8::Handle<Value>GetTextContent(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)info.Holder()->GetPointerFromInternalField(0);
return handleScope.Close(String::New([[node stringValue] UTF8String]));
@@ -200,14 +202,14 @@ static v8::Handle<Value>GetTextContent(Local<String> name, const AccessorInfo& i
static v8::Handle<Value>GetNodeType(Local<String> name, const AccessorInfo& info)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
return handleScope.Close(v8::Integer::New(1));
}
static v8::Handle<Value> InsertBefore(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if ([holder isKindOfClass:[NSXMLElement class]]) {
@@ -233,7 +235,7 @@ static v8::Handle<Value> InsertBefore(const Arguments& args)
static v8::Handle<Value> ReplaceChild(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if ([holder isKindOfClass:[NSXMLElement class]]) {
@@ -252,7 +254,7 @@ static v8::Handle<Value> ReplaceChild(const Arguments& args)
static v8::Handle<Value> RemoveChild(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if ([holder isKindOfClass:[NSXMLElement class]]) {
@@ -270,7 +272,7 @@ static v8::Handle<Value> RemoveChild(const Arguments& args)
static v8::Handle<Value> AppendChild(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if ([holder isKindOfClass:[NSXMLElement class]]) {
@@ -290,7 +292,7 @@ static v8::Handle<Value> AppendChild(const Arguments& args)
static v8::Handle<Value> HasChildNodes(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
bool ret = false;
NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
@@ -301,7 +303,7 @@ static v8::Handle<Value> HasChildNodes(const Arguments& args)
static v8::Handle<Value> Normalize(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if (holder && [holder isKindOfClass:[NSXMLElement class]])
@@ -311,7 +313,7 @@ static v8::Handle<Value> Normalize(const Arguments& args)
static v8::Handle<Value> IsSupported(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if (holder && [holder isKindOfClass:[NSXMLElement class]]) {
@@ -331,7 +333,7 @@ static v8::Handle<Value> IsSupported(const Arguments& args)
static v8::Handle<Value> GetFeature(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
id holder = (id)args.Holder()->GetPointerFromInternalField(0);
if (holder && [holder isKindOfClass:[NSXMLElement class]]) {
@@ -351,7 +353,7 @@ static v8::Handle<Value> GetFeature(const Arguments& args)
static v8::Handle<Value> IsSameNode(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
BOOL ret = NO;
HandleScope handleScope;
NSXMLNode *holder = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
@@ -363,7 +365,7 @@ static v8::Handle<Value> IsSameNode(const Arguments& args)
static v8::Handle<Value> IsEqualNode(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
BOOL ret = NO;
HandleScope handleScope;
NSXMLNode *holder = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
@@ -375,7 +377,7 @@ static v8::Handle<Value> IsEqualNode(const Arguments& args)
static v8::Handle<Value> SetUserData(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
v8::Handle<Object> obj = args.Holder();
if (args.Length() >= 2 && args[0]->IsString()) {
@@ -387,7 +389,7 @@ static v8::Handle<Value> SetUserData(const Arguments& args)
static v8::Handle<Value> GetUserData(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
v8::Handle<Object> obj = args.Holder();
if (args.Length() >= 1 && args[0]->IsString())
@@ -397,7 +399,7 @@ static v8::Handle<Value> GetUserData(const Arguments& args)
static v8::Handle<Value> LookupPrefix(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
v8::Handle<Object> obj = args.Holder();
NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
@@ -416,7 +418,7 @@ static v8::Handle<Value> LookupPrefix(const Arguments& args)
static v8::Handle<Value> IsDefaultNamespace(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
BOOL ret = NO;
v8::Handle<Object> obj = args.Holder();
@@ -436,7 +438,7 @@ static v8::Handle<Value> IsDefaultNamespace(const Arguments& args)
static v8::Handle<Value> LookupNamespaceURI(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
v8::Handle<Object> obj = args.Holder();
NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
@@ -462,7 +464,7 @@ const unsigned short DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20;
*/
static v8::Handle<Value> CompareDocumentPosition(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
/*
v8::Handle<Object> obj = args.Holder();
@@ -482,7 +484,7 @@ static void GatherElementsByName(NSXMLNode *node, char *name, NSMutableArray *el
static v8::Handle<Value> GetElementsByTagName(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
v8::Handle<Object> obj = args.Holder();
NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
@@ -512,7 +514,7 @@ static v8::Handle<Value> GetElementsByTagName(const Arguments& args)
static v8::Handle<Value> GetAttribute(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
v8::Handle<Value> ret = Undefined();
@@ -535,7 +537,7 @@ static v8::Handle<Value> GetAttribute(const Arguments& args)
static v8::Handle<Value> SetAttribute(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
if (args.Length() > 1 && [node isKindOfClass:[NSXMLElement class]]) {
@@ -556,7 +558,7 @@ static v8::Handle<Value> SetAttribute(const Arguments& args)
static v8::Handle<Value> AddEventListener(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
if (args.Length() > 2 && args[0]->IsString() && args[1]->IsFunction()
@@ -580,7 +582,7 @@ static v8::Handle<Value> AddEventListener(const Arguments& args)
static v8::Handle<Value> RemoveEventListener(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
HandleScope handleScope;
//NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
if (args.Length() > 2 && args[0]->IsString() && args[1]->IsObject()
@@ -603,7 +605,7 @@ static v8::Handle<Value> RemoveEventListener(const Arguments& args)
static v8::Handle<Value> DispatchEvent(const Arguments& args)
{
- //v8::Locker lock;
+ v8::Locker lock;
BOOL ret = NO;
HandleScope handleScope;
//NSXMLNode *node = (NSXMLNode *)args.Holder()->GetPointerFromInternalField(0);
diff --git a/javascript_examples/draw_pixels.js b/javascript_examples/draw_pixels.js
index 3227d19..b3336ad 100644
--- a/javascript_examples/draw_pixels.js
+++ b/javascript_examples/draw_pixels.js
@@ -38,7 +38,6 @@ mainloop = function() {
point = new Point(rand()%width, rand()%height);
drawer.drawPixel(point, color);
}
- sleep(1/60);
};
-run(mainloop);
+addToRunLoop(mainloop, 1/60);