summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrea Guzzo <andrea.guzzo@booking.com>2013-07-11 15:09:30 (GMT)
committer Andrea Guzzo <andrea.guzzo@booking.com>2013-07-11 15:09:30 (GMT)
commit97deef17a64bacf8bb4e187192022bacba9b79b1 (patch)
tree09479d8098bf0c3faeb6b13348f4254f81835144
parentd41d45a7cf4717f7bf9dc80204b9299d7c851a2e (diff)
fixed both node.js and jmx.js initialization
more work on the DOM Event interface while trying to obtain a working XMLHttpRequestEventTarget implementation
-rw-r--r--JMX.xcodeproj/project.pbxproj2
-rw-r--r--core/JMXCanvasPattern.h2
-rw-r--r--core/JMXDrawPath.h1
-rw-r--r--core/JMXEvent.h4
-rw-r--r--core/JMXEvent.mm59
-rw-r--r--core/JMXGraph.mm22
-rw-r--r--core/JMXPin.mm5
-rw-r--r--core/JMXScript.mm32
-rw-r--r--core/JMXV8.h18
-rw-r--r--core/NSFont+V8.h2
-rw-r--r--core/NSObject+V8.mm9
-rw-r--r--core/NSXMLNode+V8.h1
-rw-r--r--jsapi/DOM.js40
-rw-r--r--jsapi/JMX.js4
-rw-r--r--node.js/node.gyp1
-rw-r--r--node.js/src/node.js1
16 files changed, 152 insertions, 51 deletions
diff --git a/JMX.xcodeproj/project.pbxproj b/JMX.xcodeproj/project.pbxproj
index fb3ce9c..55ef2d5 100644
--- a/JMX.xcodeproj/project.pbxproj
+++ b/JMX.xcodeproj/project.pbxproj
@@ -1249,7 +1249,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
- shellScript = "cd ${PROJECT_DIR}/node.js; make; cd -;";
+ shellScript = "cd ${PROJECT_DIR}/node.js; make; cd -;\n$PROJECT_DIR/node.js/tools/js2c.py $PROJECT_DIR/core/node_natives_jmx.h $PROJECT_DIR/jsapi/*.js";
};
/* End PBXShellScriptBuildPhase section */
diff --git a/core/JMXCanvasPattern.h b/core/JMXCanvasPattern.h
index 5835f98..25597a0 100644
--- a/core/JMXCanvasPattern.h
+++ b/core/JMXCanvasPattern.h
@@ -25,8 +25,6 @@
@property (readonly) JMXRect *rect;
@property (readonly) CGImage *image;
-- (id)jmxInit;
-
+ (id)patternWithBounds:(NSRect)bounds xStep:(NSUInteger)xStep yStep:(NSUInteger)yStep tiling:(CGPatternTiling)tilingMode isColored:(BOOL)isColored;
- (id)initWithBounds:(NSRect)bounds xStep:(NSUInteger)xStep yStep:(NSUInteger)yStep tiling:(CGPatternTiling)tilingMode isColored:(BOOL)isColored;
- (CGPatternRef)patternRef;
diff --git a/core/JMXDrawPath.h b/core/JMXDrawPath.h
index 2b78c1a..1adce6b 100644
--- a/core/JMXDrawPath.h
+++ b/core/JMXDrawPath.h
@@ -78,7 +78,6 @@
@property (readwrite, copy) JMXSize *frameSize;
@property (readwrite, assign) CGFloat lineWidth;
@property (assign) BOOL invertYCoordinates;
-- (id)jmxInit;
/*!
@method drawPathWithFrameSize
diff --git a/core/JMXEvent.h b/core/JMXEvent.h
index 34c0b9c..2d65871 100644
--- a/core/JMXEvent.h
+++ b/core/JMXEvent.h
@@ -108,3 +108,7 @@ typedef enum {
capture:(BOOL)capture;
@end
+
+#ifdef __JMXV8__
+JMXV8_DECLARE_CONSTRUCTOR(JMXEvent);
+#endif
diff --git a/core/JMXEvent.mm b/core/JMXEvent.mm
index c2dc532..4c96c13 100644
--- a/core/JMXEvent.mm
+++ b/core/JMXEvent.mm
@@ -10,6 +10,8 @@
#import "JMXEvent.h"
#import "JMXScript.h"
+JMXV8_EXPORT_CLASS(JMXEvent);
+
using namespace v8;
@implementation JMXEvent
@@ -57,9 +59,36 @@ using namespace v8;
}
+
#pragma mark V8
-static v8::Persistent<FunctionTemplate> objectTemplate;
+- (void)_initInternal:(v8::Arguments *)args
+{
+ if (args->Length() >= 1) {
+ v8::Handle<Value> arg = (*args)[0];
+ v8::String::Utf8Value evtType(arg);
+ self.type = [NSString stringWithFormat:@"%s", *evtType];
+ }
+}
+
+- (void)jsInit:(NSValue *)argsValue
+{
+ v8::Locker lock;
+ HandleScope handleScope;
+ v8::Arguments *args = (v8::Arguments *)[argsValue pointerValue];
+ [self _initInternal:args];
+}
+
+static v8::Handle<Value>InitEvent(const Arguments& args)
+{
+ v8::Locker lock;
+ HandleScope handleScope;
+ if (args.Length() >= 1) {
+ JMXEvent *event = (JMXEvent *)args.Holder()->GetPointerFromInternalField(0);
+ [event _initInternal:(v8::Arguments *)&args];
+ }
+ return handleScope.Close(Undefined());
+}
+ (v8::Persistent<FunctionTemplate>)jsObjectTemplate
{
@@ -74,6 +103,8 @@ static v8::Persistent<FunctionTemplate> objectTemplate;
objectTemplate->SetClassName(String::New("Event"));
v8::Handle<ObjectTemplate> classProto = objectTemplate->PrototypeTemplate();
// set instance methods
+ classProto->Set("initEvent", FunctionTemplate::New(InitEvent));
+
v8::Handle<ObjectTemplate> instanceTemplate = objectTemplate->InstanceTemplate();
instanceTemplate->SetInternalFieldCount(1);
// Add accessors for each of the fields of the entity.
@@ -88,19 +119,19 @@ static v8::Persistent<FunctionTemplate> objectTemplate;
return objectTemplate;
}
-static void JMXEventJSDestructor(Persistent<Value> object, void *parameter)
-{
- HandleScope handle_scope;
- Locker lock;
- JMXEvent *obj = static_cast<JMXEvent *>(parameter);
- //NSLog(@"V8 WeakCallback (Rect) called %@", obj);
- [obj release];
- if (!object.IsEmpty()) {
- object.ClearWeak();
- object.Dispose();
- object.Clear();
- }
-}
+//static void JMXEventJSDestructor(Persistent<Value> object, void *parameter)
+//{
+// HandleScope handle_scope;
+// Locker lock;
+// JMXEvent *obj = static_cast<JMXEvent *>(parameter);
+// //NSLog(@"V8 WeakCallback (Rect) called %@", obj);
+// [obj release];
+// if (!object.IsEmpty()) {
+// object.ClearWeak();
+// object.Dispose();
+// object.Clear();
+// }
+//}
- (Handle<Object>)jsObj
{
diff --git a/core/JMXGraph.mm b/core/JMXGraph.mm
index 7c1fe17..3285410 100644
--- a/core/JMXGraph.mm
+++ b/core/JMXGraph.mm
@@ -12,6 +12,7 @@
#import "JMXElement.h"
#import "JMXCanvasElement.h"
#import "NSXMLNode+V8.h"
+#import "JMXEvent.h"
@implementation JMXGraph
@@ -147,6 +148,23 @@ static v8::Handle<Value> GetElementById(const Arguments& args)
return Undefined();
}
+static v8::Handle<Value> CreateEvent(const Arguments& args)
+{
+ v8::Locker lock;
+ HandleScope handleScope;
+ if (args.Length() >= 1) {
+ v8::String::Utf8Value eventType(args[0]);
+ if (strcasecmp(*eventType, "Event") == 0) {
+ JMXEvent *event = [[[JMXEvent alloc] init] autorelease];
+ return handleScope.Close([event jsObj]);
+ } else {
+ // TODO - throw exception
+ NSLog(@"Unknown event type: %s", *eventType);
+ }
+ }
+ return Undefined();
+}
+
static v8::Handle<Value> MapSet(Local<String> name, Local<Value> value, const AccessorInfo &info)
{
v8::Locker lock;
@@ -232,12 +250,12 @@ static void JMXGraphJSDestructor(Persistent<Value> object, void *parameter)
return objectTemplate;
objectTemplate = v8::Persistent<FunctionTemplate>::New(FunctionTemplate::New());
objectTemplate->Inherit([super jsObjectTemplate]);
- objectTemplate->SetClassName(String::New("Graph"));
+ objectTemplate->SetClassName(String::New("Document"));
v8::Handle<ObjectTemplate> classProto = objectTemplate->PrototypeTemplate();
classProto->Set("createElement", FunctionTemplate::New(CreateElement));
classProto->Set("createComment", FunctionTemplate::New(CreateComment));
classProto->Set("getElementById", FunctionTemplate::New(GetElementById));
-
+ classProto->Set("createEvent", FunctionTemplate::New(CreateEvent));
v8::Handle<ObjectTemplate> instanceTemplate = objectTemplate->InstanceTemplate();
instanceTemplate->SetAccessor(String::NewSymbol("uid"), GetStringProperty, SetStringProperty);
diff --git a/core/JMXPin.mm b/core/JMXPin.mm
index 1a0a7a6..97ac072 100644
--- a/core/JMXPin.mm
+++ b/core/JMXPin.mm
@@ -876,14 +876,17 @@ static v8::Persistent<FunctionTemplate> objectTemplate;
objectTemplate = Persistent<FunctionTemplate>::New(FunctionTemplate::New());
objectTemplate->Inherit([super jsObjectTemplate]);
objectTemplate->SetClassName(String::New("Pin"));
+
+ // set instance methods
v8::Handle<ObjectTemplate> classProto = objectTemplate->PrototypeTemplate();
classProto->Set("connect", FunctionTemplate::New(connect));
classProto->Set("disconnect", FunctionTemplate::New(disconnect));
classProto->Set("disconnectAll", FunctionTemplate::New(disconnectAll));
classProto->Set("export", FunctionTemplate::New(exportToBoard));
- // set instance methods
+
v8::Handle<ObjectTemplate> instanceTemplate = objectTemplate->InstanceTemplate();
instanceTemplate->SetInternalFieldCount(1);
+
// Add accessors for each of the fields of the entity.
instanceTemplate->SetAccessor(String::NewSymbol("label"), GetStringProperty, SetStringProperty);
instanceTemplate->SetAccessor(String::NewSymbol("type"), type);
diff --git a/core/JMXScript.mm b/core/JMXScript.mm
index aae2688..71cd34b 100644
--- a/core/JMXScript.mm
+++ b/core/JMXScript.mm
@@ -56,6 +56,9 @@
#import "NSNumber+V8.h"
#import "NSObject+V8.h"
+#include "node_natives_jmx.h"
+#include "node_String.h"
+
using namespace v8;
using namespace std;
using namespace node;
@@ -106,6 +109,7 @@ static JMXV8ClassDescriptor mappedClasses[] = {
{ "JMXScriptOutputPin", "OutputPin", JMXOutputPinJSConstructor },
{ "JMXTextEntity", "TextEntity", JMXTextEntityJSConstructor },
{ "JMXHIDInputEntity", "HIDInput", JMXHIDInputEntityJSConstructor },
+ { "JMXEvent", "Event", JMXEventJSConstructor },
{ NULL, NULL, NULL }
};
@@ -662,10 +666,10 @@ static char *argv[2] = { (char *)"JMX", NULL };
// continue;
// }
Class entityClass = NSClassFromString([NSString stringWithUTF8String:mappedClasses[i].className]);
- if (entityClass) {
+ if ([entityClass respondsToSelector:@selector(jsRegisterClassMethods:)]) {
[entityClass jsRegisterClassMethods:constructor];
- ctxTemplate->Set(String::New(mappedClasses[i].jsClassName), constructor);
}
+ ctxTemplate->Set(String::New(mappedClasses[i].jsClassName), constructor);
}
[pool drain];
}
@@ -784,17 +788,33 @@ static char *argv[2] = { (char *)"JMX", NULL };
operationQueue = [[NSOperationQueue alloc] init];
+
// second part of node initialization
Handle<Object> process = node::SetupProcessObject(1, argv);
v8_typed_array::AttachBindings(ctx->Global());
// Create all the objects, load modules, do everything.
- // so your next reading stop should be node::Load()!
node::Load(process);
- char baseInclude[] = "include('JMX.js');";
- // Enter the newly created execution environment.
- ExecJSCode(baseInclude, strlen(baseInclude), "JMX");
+ // and now load the JMX native library (includes processing and jquery)
+ for (int i = 0; natives[i].name; i++) {
+ Local<String> name = String::New(natives[i].name);
+ Handle<String> source = BUILTIN_ASCII_ARRAY(natives[i].source, natives[i].source_len);
+
+ TryCatch try_catch;
+
+ Local<v8::Script> script = v8::Script::Compile(source, name);
+ if (script.IsEmpty()) {
+ ReportException(&try_catch);
+ // exit(3);
+ }
+
+ Local<Value> result = script->Run();
+ if (result.IsEmpty()) {
+ ReportException(&try_catch);
+ //exit(4);
+ }
+ }
}
- (void)clearPersistentInstances
diff --git a/core/JMXV8.h b/core/JMXV8.h
index cce4bad..6105b7f 100644
--- a/core/JMXV8.h
+++ b/core/JMXV8.h
@@ -54,7 +54,6 @@
*/
- (v8::Handle<v8::Object>)jsObj;
-
@optional
/*!
@@ -77,10 +76,12 @@
@method jsRegisterClassMethods:
@param constructor The constructor FunctionTemplate where to attach class methods
@discussion If implemented, this message will be called when the constructor is registered
- into the javascript global context.
+ into the javascript global context.
*/
+ (void)jsRegisterClassMethods:(v8::Handle<v8::FunctionTemplate>)constructor;
+- (id)jmxInit;
+
/*!
@method jsInit:
@param argsValue arguments passed to the constructor
@@ -153,11 +154,14 @@ v8::Handle<Value> __class##JSConstructor(const Arguments& args)\
if (objectTemplate.IsEmpty())\
objectTemplate = [__class jsObjectTemplate];\
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];\
- __class *instance = nil;\
v8::Local<Context> currentContext = v8::Context::GetCurrent();\
JMXScript *ctx = [JMXScript getContext];\
if (ctx) {\
- instance = [[__class alloc] jmxInit];\
+ __class *instance = [__class alloc];\
+ if ([instance respondsToSelector:@selector(jmxInit)])\
+ instance = [instance jmxInit];\
+ else\
+ instance = [instance init];\
/* connect the entity to our scriptEntity */\
if ([instance isKindOfClass:[JMXElement class]]) {\
[ctx.scriptEntity performSelector:@selector(hookEntity:) withObject:instance];\
@@ -206,7 +210,11 @@ v8::Handle<Value> __class##JSConstructor(const Arguments& args)\
if (objectTemplate.IsEmpty())\
objectTemplate = [__class jsObjectTemplate];\
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];\
- __class *instance = [[__class alloc] jmxInit];\
+ __class *instance = [__class alloc];\
+ if ([instance respondsToSelector:@selector(jmxInit)])\
+ instance = [instance jmxInit];\
+ else\
+ instance = [instance init];\
/* connect the entity to our scriptEntity */\
if ([instance respondsToSelector:@selector(jsInit:)]) {\
NSValue *argsValue = [NSValue valueWithPointer:(void *)&args];\
diff --git a/core/NSFont+V8.h b/core/NSFont+V8.h
index c0c6789..81e8ee5 100644
--- a/core/NSFont+V8.h
+++ b/core/NSFont+V8.h
@@ -11,8 +11,6 @@
@interface NSFont (JMXV8) <JMXV8>
-- (id)jmxInit;
-
JMXV8_DECLARE_CONSTRUCTOR(NSXMLFont);
@end
diff --git a/core/NSObject+V8.mm b/core/NSObject+V8.mm
index 5e7ce92..81f642f 100644
--- a/core/NSObject+V8.mm
+++ b/core/NSObject+V8.mm
@@ -16,6 +16,10 @@ using namespace v8;
static Persistent<FunctionTemplate> objectTemplate;
++ (void)jsRegisterClassMethods:(v8::Handle<v8::FunctionTemplate>)constructor
+{
+ // do nothing for now
+}
+ (v8::Persistent<FunctionTemplate>)jsObjectTemplate
{
@@ -63,4 +67,9 @@ static void JMXObjectJSDestructor(Persistent<Value> object, void *parameter)
return handle_scope.Close(jsInstance);
}
+- (id)jmxInit
+{
+ return [self init];
+}
+
@end
diff --git a/core/NSXMLNode+V8.h b/core/NSXMLNode+V8.h
index 05a8a8b..9119e9a 100644
--- a/core/NSXMLNode+V8.h
+++ b/core/NSXMLNode+V8.h
@@ -42,7 +42,6 @@
@interface NSXMLNode (JMXV8) <JMXV8>
-- (id)jmxInit;
- (NSString *)hashString;
JMXV8_DECLARE_NODE_CONSTRUCTOR(NSXMLNode);
diff --git a/jsapi/DOM.js b/jsapi/DOM.js
index adb7179..1f460ee 100644
--- a/jsapi/DOM.js
+++ b/jsapi/DOM.js
@@ -526,7 +526,6 @@ navigator = new Navigator(); // XXX
location = new Object();
-
// states
UNSENT = 0;
OPENED = 1;
@@ -534,10 +533,11 @@ HEADERS_RECEIVED = 2;
LOADING = 3;
DONE = 4;
-var http = require('http');
-var url = require('url');
-
function XMLHttpRequest() {
+ var http = require('http');
+ var url = require('url');
+ //var assert = require('assert');
+
this.open = function(method, url, async, user, password) {
this.method = method;
this.url = url;
@@ -545,6 +545,7 @@ function XMLHttpRequest() {
this.user = user;
this.password = password;
this.headers = { };
+ Object.defineProperty(this, "status", { value: OPENED, writable: false, enumerable: false });
}
this.setRequestHeader = function(header, value) {
@@ -555,26 +556,39 @@ function XMLHttpRequest() {
if (this.status != OPENED)
throw(new DOMException(INVALID_STATE_ERR));
var agent = new http.Agent({ port: 80, maxSockets: 1 });
- if (method.toLowerCase() == "get") {
+ if (this.method.toLowerCase() == "get") {
- } else if (method.toLowerCase() == "post") {
+ } else if (this.method.toLowerCase() == "post") {
}
+
+ Object.defineProperty(this, "status", { value: LOADING, writable: false, enumerable: false });
+ if (this.onreadystatechange) {
+ //e = new Event();
+ //e.type =
+ e = document.createEvent();
+ e.initEvent('readystatechange', true, false);
+ }
+ var body0 = '';
http.get({
- port: common.PORT,
- path: '/hello',
+ host: 'q-ec.bstatic.com',
+ port: 80,
+ path: '/static/css/main_edgecast.3836.css',
headers: {'Accept': '*/*', 'Foo': 'bar'},
agent: agent
}, function(res) {
- assert.equal(200, res.statusCode);
- responses_recvd += 1;
+ Object.defineProperty(this, "status", { value: HEADERS_RECEIVED, writable: false, enumerable: false });
+ //assert.equal(200, res.statusCode);
+ //responses_recvd += 1;
res.setEncoding('utf8');
- res.on('data', function(chunk) { body0 += chunk; });
- common.debug('Got /hello response');
+ res.on('data', function(chunk) { body0 += chunk; });
+ res.on('end', function() { if (this.onreadystatechange) this.onreadystatechange() });
+ //echo('got response');
+ //common.debug('Got /hello response');
});
}
Object.defineProperty(this, "onreadystatechange", { value: function() { }, writable: true, enumerable: false });
- Object.defineProperty(this, "status", { value: 0, writable: false, enumerable: false });
+ Object.defineProperty(this, "status", { value: UNSENT, writable: false, enumerable: false, configurable: true });
Object.defineProperty(this, "statusText", { value: "", writable: false, enumerable: false });
}
diff --git a/jsapi/JMX.js b/jsapi/JMX.js
index 6c4a8a8..e1c082a 100644
--- a/jsapi/JMX.js
+++ b/jsapi/JMX.js
@@ -6,7 +6,7 @@
*/
//echo("Initializing JMX Core API");
-
+/*
include("VideoOutput.js");
//echo("Registered class: VideoOutput");
include("VideoFilter.js");
@@ -22,7 +22,7 @@ include("AudioFile.js");
include("DOM.js"); // DOM related additions
include("jquery.js");
include("processing.js");
-
+*/
/**
* Extend the internal Pin class to allow 'stringifying' them
* (so that "echo(array_variable)" will dump its content)
diff --git a/node.js/node.gyp b/node.js/node.gyp
index 579511b..0829677 100644
--- a/node.js/node.gyp
+++ b/node.js/node.gyp
@@ -58,6 +58,7 @@
'lib/util.js',
'lib/vm.js',
'lib/zlib.js',
+ 'lib/_third_party_main.js'
],
},
diff --git a/node.js/src/node.js b/node.js/src/node.js
index 24fcb0b..e5833cb 100644
--- a/node.js/src/node.js
+++ b/node.js/src/node.js
@@ -61,7 +61,6 @@
// are running from a script and running the REPL - but there are a few
// others like the debugger or running --eval arguments. Here we decide
// which mode we run in.
- return;
if (NativeModule.exists('_third_party_main')) {
// To allow people to extend Node in different ways, this hook allows