summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrea Guzzo <andrea.guzzo@booking.com>2013-09-27 15:12:40 (GMT)
committer Andrea Guzzo <andrea.guzzo@booking.com>2013-09-27 15:12:40 (GMT)
commit03ffe7d7ac5a17095dc4ff68a28f7f25d40368e1 (patch)
tree25fdeced4ddc76d4da91e4d2ef54f34234ca1ab9
parent55b7afafc41d6cfd0244b7ee251d5f323ffbf5b5 (diff)
expose load() in the global context
and return the newly created scriptentity if success , null otherwise
-rw-r--r--core/JMXScript.mm82
-rw-r--r--entities/JMXScriptEntity.h2
-rw-r--r--entities/JMXScriptEntity.mm17
-rw-r--r--entities/JMXScriptFile.mm4
4 files changed, 39 insertions, 66 deletions
diff --git a/core/JMXScript.mm b/core/JMXScript.mm
index 12bbea6..b1e846f 100644
--- a/core/JMXScript.mm
+++ b/core/JMXScript.mm
@@ -332,6 +332,27 @@ static v8::Handle<Value> Include(const Arguments& args) {
return scope.Close(v8::Boolean::New(ret ? 1 : 0));
}
+static v8::Handle<Value> Load(const Arguments& args) {
+ if (args.Length() < 1)
+ return v8::Undefined();
+ //v8::Locker lock;
+ HandleScope scope;
+ BOOL ret = NO;
+ v8::Handle<Value> arg = args[0];
+ v8::String::Utf8Value value(arg);
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ NSString *path = [NSString stringWithUTF8String:*value];
+ Local<Context> c = v8::Context::GetCurrent();
+ Local<Object> globalObject = c->Global();
+ v8::Local<v8::Object> obj = globalObject->Get(v8::String::New("scriptEntity"))->ToObject();
+ JMXScriptEntity *entity = (JMXScriptEntity *)obj->GetPointerFromInternalField(0);
+ JMXScriptFile *script = [entity load:path];
+ [pool release];
+ if (script)
+ return scope.Close([script jsObj]);
+ return scope.Close(Undefined());
+}
+
static v8::Handle<Value> ListEntities(const Arguments& args)
{
//v8::Locker lock;
@@ -366,66 +387,6 @@ static v8::Handle<Value> Sleep(const Arguments& args)
return handleScope.Close(v8::Boolean::New(YES));
}
-//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)
{
HandleScope handleScope;
@@ -789,6 +750,7 @@ static Persistent<ObjectTemplate> ctxTemplate;
ctxTemplate->Set(String::New("echo"), FunctionTemplate::New(Echo));
ctxTemplate->Set(String::New("print"), FunctionTemplate::New(Echo));
ctxTemplate->Set(String::New("include"), FunctionTemplate::New(Include));
+ ctxTemplate->Set(String::New("load"), FunctionTemplate::New(Load));
ctxTemplate->Set(String::New("sleep"), FunctionTemplate::New(Sleep));
ctxTemplate->Set(String::New("lsdir"), FunctionTemplate::New(ListDir));
ctxTemplate->Set(String::New("isdir"), FunctionTemplate::New(IsDir));
diff --git a/entities/JMXScriptEntity.h b/entities/JMXScriptEntity.h
index 1d82e0c..3b83f00 100644
--- a/entities/JMXScriptEntity.h
+++ b/entities/JMXScriptEntity.h
@@ -13,6 +13,7 @@
@class JMXScriptInputPin;
@class JMXScriptOutputPin;
@class JMXScriptPinWrapper;
+@class JMXScriptFile;
@interface JMXScriptEntity : JMXEntity {
@protected
@@ -33,6 +34,7 @@
- (BOOL)exec:(NSString *)code;
- (void)resetContext;
- (void)hookEntity:(JMXEntity *)entity;
+- (JMXScriptFile *)load:(NSString *)path;
#ifdef __JMXV8__
- (JMXScriptPinWrapper *)wrapPin:(JMXPin *)pin withFunction:(v8::Persistent<v8::Function>)function;
diff --git a/entities/JMXScriptEntity.mm b/entities/JMXScriptEntity.mm
index 0a642c8..5e16561 100644
--- a/entities/JMXScriptEntity.mm
+++ b/entities/JMXScriptEntity.mm
@@ -203,8 +203,12 @@ using namespace v8;
- (JMXScriptFile *)load:(NSString *)path
{
JMXScriptFile *script = [[JMXScriptFile alloc] init];
- [self hookEntity:script];
script.path = path;
+ if (![script.path isEqualToString:path]) {
+ [script release];
+ return nil;
+ }
+ [self hookEntity:script];
return script;
}
@@ -305,19 +309,22 @@ static Persistent<FunctionTemplate> objectTemplate;
static v8::Handle<Value>LoadScript(const Arguments& args)
{
//v8::Locker lock;
- BOOL ret = NO;
+ JMXScriptFile *script = nil;;
HandleScope handleScope;
JMXScriptEntity *entity = (JMXScriptEntity *)args.Holder()->GetPointerFromInternalField(0);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (args[0]->IsString()) {
String::Utf8Value str(args[0]->ToString());
- if ([entity load:[NSString stringWithUTF8String:*str]])
- ret = YES;
+ script = [entity load:[NSString stringWithUTF8String:*str]];
} else {
NSLog(@"ScriptEntity::load(): argument is not a string");
}
[pool release];
- return handleScope.Close(v8::Boolean::New(ret ? 1 : 0));
+
+ if (script)
+ return handleScope.Close([script jsObj]);
+
+ return handleScope.Close(Undefined());
}
static v8::Handle<Value>Exec(const Arguments& args)
diff --git a/entities/JMXScriptFile.mm b/entities/JMXScriptFile.mm
index d1ee966..98b90ad 100644
--- a/entities/JMXScriptFile.mm
+++ b/entities/JMXScriptFile.mm
@@ -71,11 +71,13 @@ extern void JSExit(int code);
if (fh) {
NSData *data = [fh readDataToEndOfFile];
self.code = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
+ // TODO - validate code
[NSString stringWithCharacters:(const unichar *)[data bytes] length:[data length]];
self.label = [[newPath componentsSeparatedByString:@"/"] lastObject];
+ return YES;
}
}
- return YES;
+ return NO;
}
- (void)close