[Pkg-javascript-commits] [node-contextify] 01/02: Import Upstream version 0.1.6

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Tue Oct 25 10:21:40 UTC 2016


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-contextify.

commit 2f8d62ac6cc965d63faf19ddc713251636215a53
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date:   Tue Oct 25 12:36:07 2016 +0530

    Import Upstream version 0.1.6
---
 .npmignore => .gitignore |   1 -
 .npmignore               |   6 +--
 Makefile                 |  12 +++++
 README.md                |   6 ++-
 changelog                |   9 ++++
 lib/contextify.js        |  22 +++++++-
 package.json             |   2 +-
 src/contextify.cc        | 129 ++++++++++++++++++++++++++++++++---------------
 test/contextify.js       |  96 ++++++++++++++++++++++++++++++++++-
 wscript                  |   3 +-
 10 files changed, 233 insertions(+), 53 deletions(-)

diff --git a/.npmignore b/.gitignore
similarity index 92%
copy from .npmignore
copy to .gitignore
index 3a7de5f..dbf6884 100644
--- a/.npmignore
+++ b/.gitignore
@@ -1,4 +1,3 @@
-Makefile
 .lock-wscript
 node_modules/
 build/
diff --git a/.npmignore b/.npmignore
index 3a7de5f..4c0b0bb 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,7 +1,7 @@
 Makefile
 .lock-wscript
-node_modules/
-build/
+node_modules
+build
 *.swp
 *.swo
 TODO
@@ -9,4 +9,4 @@ Makefile.gyp
 *.Makefile
 *.target.gyp.mk
 gyp-mac-tool
-out/
+out
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..6af8c72
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+all: build
+
+.PHONY: test clean
+
+clean:
+	node-waf distclean
+
+build: src/contextify.cc
+	node-waf distclean && node-waf configure build
+
+test:
+	npm test
diff --git a/README.md b/README.md
index 71c45e9..6a713a1 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,10 @@
 # Contextify
 
-Turn an object into a V8 execution context.  A contextified object acts as the global 'this' when executing scripts in its context.  Contextify adds 3 methods to the contextified object: run(code, filename), getGlobal(), and dispose().  The main difference between Contextify and Node's vm methods is that Contextify allows asynchronous functions to continue executing in the Contextified object's context.  See vm vs. Contextify below for more discussion.
+For Windows issues, see here: https://github.com/brianmcd/contextify/wiki/Windows-Installation-Guide
+
+Please add to the wiki if you find new issues/solutions.
 
-NOTE: This module will not install from npm on Windows.  See https://github.com/brianmcd/contextify/issues/10 for more info.
+Turn an object into a V8 execution context.  A contextified object acts as the global 'this' when executing scripts in its context.  Contextify adds 3 methods to the contextified object: run(code, filename), getGlobal(), and dispose().  The main difference between Contextify and Node's vm methods is that Contextify allows asynchronous functions to continue executing in the Contextified object's context.  See vm vs. Contextify below for more discussion.
 
 ## Examples
 ```javascript
diff --git a/changelog b/changelog
index f6f8c1a..244764c 100644
--- a/changelog
+++ b/changelog
@@ -1,3 +1,12 @@
+0.1.6
+    * Fix broken build with node >= 0.11.0 (Jamie Kirkpatrick - @jpk)
+0.1.5
+    * Fix broken builds on 0.9.11 and above (tomgco - #61)
+0.1.4
+    * Fix segfault on Node >= 0.9.6. (pyokagan)
+    * Allow pre-compiling scripts. (mroch)
+0.1.3
+    * Remove PrintException.
 0.1.2
     * Renamed bindings.gyp to binding.gyp (Rex Morgan)
     * More fixes for OS X build.
diff --git a/lib/contextify.js b/lib/contextify.js
index a65fb53..01020e0 100644
--- a/lib/contextify.js
+++ b/lib/contextify.js
@@ -1,6 +1,8 @@
-var ContextifyContext = require('bindings')('contextify').ContextifyContext;
+var binding = require('bindings')('contextify');
+var ContextifyContext = binding.ContextifyContext;
+var ContextifyScript = binding.ContextifyScript;
 
-module.exports = function Contextify (sandbox) {
+function Contextify (sandbox) {
     if (typeof sandbox != 'object') {
         sandbox = {};
     }
@@ -28,3 +30,19 @@ module.exports = function Contextify (sandbox) {
     }
     return sandbox;
 }
+
+Contextify.createContext = function (sandbox) {
+    if (typeof sandbox != 'object') {
+        sandbox = {};
+    }
+    return new ContextifyContext(sandbox);
+};
+
+Contextify.createScript = function (code, filename) {
+    if (typeof code != 'string') {
+        throw new TypeError('Code argument is required');
+    }
+    return new ContextifyScript(code, filename);
+};
+
+module.exports = Contextify;
diff --git a/package.json b/package.json
index e498417..60c5f4a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
     "name": "contextify",
-    "version": "0.1.2",
+    "version": "0.1.6",
     "description": "Turn an object into a persistent execution context.",
     "author": "Brian McDaniel <brianmcd05 at gmail.com>",
     "contributors": [ 
diff --git a/src/contextify.cc b/src/contextify.cc
index cbff2f9..78402aa 100644
--- a/src/contextify.cc
+++ b/src/contextify.cc
@@ -1,4 +1,5 @@
 #include "node.h"
+#include "node_version.h"
 #include <string>
 using namespace v8;
 using namespace node;
@@ -48,7 +49,11 @@ public:
     Local<Object> createDataWrapper () {
         HandleScope scope;
         Local<Object> wrapper = dataWrapperCtor->NewInstance();
+#if NODE_MAJOR_VERSION > 0 || (NODE_MINOR_VERSION == 9 && (NODE_PATCH_VERSION >= 6 && NODE_PATCH_VERSION <= 10)) || NODE_MINOR_VERSION >= 11
+        wrapper->SetAlignedPointerInInternalField(0, this);
+#else
         wrapper->SetPointerInInternalField(0, this);
+#endif
         return scope.Close(wrapper);
     }
 
@@ -124,18 +129,20 @@ public:
         }
         if (script.IsEmpty()) {
           context->Exit();
-          PrintException(trycatch);
           return trycatch.ReThrow();
         }
         Handle<Value> result = script->Run();
         context->Exit();
         if (result.IsEmpty()) {
-            PrintException(trycatch);
             return trycatch.ReThrow();
         }
         return scope.Close(result);
     }
 
+    static bool InstanceOf(Handle<Value> value) {
+      return !value.IsEmpty() && jsTmpl->HasInstance(value);
+    }
+
     static Handle<Value> GetGlobal(const Arguments& args) {
         HandleScope scope;
         ContextifyContext* ctx = ObjectWrap::Unwrap<ContextifyContext>(args.This());
@@ -208,56 +215,98 @@ public:
         ContextifyContext* ctx = ObjectWrap::Unwrap<ContextifyContext>(data);
         return scope.Close(ctx->sandbox->GetPropertyNames());
     }
+};
 
-    // Mostly taken from d8 shell.
-    static void PrintException(TryCatch &try_catch) {
-        HandleScope handle_scope;
-        String::Utf8Value exception(try_catch.Exception());
-        const char* exception_string = ToCString(exception);
-        Handle<v8::Message> message = try_catch.Message();
-        if (message.IsEmpty()) {
-            // V8 didn't provide any extra information about this error; just
-            // print the exception.
-            fprintf(stderr, "%s\n", exception_string);
-        } else {
-            // Print (filename):(line number): (message).
-            String::Utf8Value filename(message->GetScriptResourceName());
-            const char* filename_string = ToCString(filename);
-            int linenum = message->GetLineNumber();
-            fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
-            // Print line of source code.
-            String::Utf8Value sourceline(message->GetSourceLine());
-            const char* sourceline_string = ToCString(sourceline);
-            fprintf(stderr, "%s\n", sourceline_string);
-            // Print wavy underline (GetUnderline is deprecated).
-            int start = message->GetStartColumn();
-            for (int i = 0; i < start; i++) {
-                fprintf(stderr, " ");
-            }
-            int end = message->GetEndColumn();
-            for (int i = start; i < end; i++) {
-                fprintf(stderr, "^");
-            }
-            fprintf(stderr, "\n");
-            String::Utf8Value stack_trace(try_catch.StackTrace());
-            if (stack_trace.length() > 0) {
-                const char* stack_trace_string = ToCString(stack_trace);
-                fprintf(stderr, "%s\n", stack_trace_string);
-            }
+class ContextifyScript : ObjectWrap {
+public:
+    static Persistent<FunctionTemplate> scriptTmpl;
+    Persistent<Script> script;
+
+    static void Init(Handle<Object> target) {
+        HandleScope scope;
+        scriptTmpl = Persistent<FunctionTemplate>::New(FunctionTemplate::New(New));
+        scriptTmpl->InstanceTemplate()->SetInternalFieldCount(1);
+        scriptTmpl->SetClassName(String::NewSymbol("ContextifyScript"));
+
+        NODE_SET_PROTOTYPE_METHOD(scriptTmpl, "runInContext", RunInContext);
+
+        target->Set(String::NewSymbol("ContextifyScript"),
+                    scriptTmpl->GetFunction());
+    }
+
+    static Handle<Value> New(const Arguments& args) {
+      HandleScope scope;
+
+      ContextifyScript *contextify_script = new ContextifyScript();
+      contextify_script->Wrap(args.Holder());
+
+      if (args.Length() < 1) {
+        return ThrowException(Exception::TypeError(
+              String::New("needs at least 'code' argument.")));
+      }
+
+      Local<String> code = args[0]->ToString();
+      Local<String> filename = args.Length() > 1
+                               ? args[1]->ToString()
+                               : String::New("ContextifyScript.<anonymous>");
+
+      Handle<Context> context = Context::GetCurrent();
+      Context::Scope context_scope(context);
+
+      // Catch errors
+      TryCatch trycatch;
+
+      Handle<Script> v8_script = Script::New(code, filename);
+
+      if (v8_script.IsEmpty()) {
+        return trycatch.ReThrow();
+      }
+
+      contextify_script->script = Persistent<Script>::New(v8_script);
+
+      return args.This();
+    }
+
+    static Handle<Value> RunInContext(const Arguments& args) {
+        HandleScope scope;
+        if (args.Length() == 0) {
+            Local<String> msg = String::New("Must supply at least 1 argument to runInContext");
+            return ThrowException(Exception::Error(msg));
+        }
+        if (!ContextifyContext::InstanceOf(args[0]->ToObject())) {
+            Local<String> msg = String::New("First argument must be a ContextifyContext.");
+            return ThrowException(Exception::TypeError(msg));
+        }
+        ContextifyContext* ctx = ObjectWrap::Unwrap<ContextifyContext>(args[0]->ToObject());
+        Persistent<Context> context = ctx->context;
+        context->Enter();
+        ContextifyScript* wrapped_script = ObjectWrap::Unwrap<ContextifyScript>(args.This());
+        Handle<Script> script = wrapped_script->script;
+        TryCatch trycatch;
+        if (script.IsEmpty()) {
+          context->Exit();
+          return trycatch.ReThrow();
+        }
+        Handle<Value> result = script->Run();
+        context->Exit();
+        if (result.IsEmpty()) {
+            return trycatch.ReThrow();
         }
+        return scope.Close(result);
     }
 
-    // Extracts a C string from a V8 Utf8Value.
-    static const char* ToCString(const String::Utf8Value& value) {
-        return *value ? *value : "<string conversion failed>";
+    ~ContextifyScript() {
+        script.Dispose();
     }
 };
 
 Persistent<FunctionTemplate> ContextifyContext::jsTmpl;
+Persistent<FunctionTemplate> ContextifyScript::scriptTmpl;
 
 extern "C" {
     static void init(Handle<Object> target) {
         ContextifyContext::Init(target);
+        ContextifyScript::Init(target);
     }
     NODE_MODULE(contextify, init);
 };
diff --git a/test/contextify.js b/test/contextify.js
index 91f0f66..49b2fc7 100644
--- a/test/contextify.js
+++ b/test/contextify.js
@@ -22,6 +22,17 @@ exports['basic tests'] = {
         test.done();
     },
 
+    'basic createContext' : function (test) {
+        var sandbox = {
+            prop1: 'prop1',
+            prop2: 'prop2'
+        };
+        var context = Contextify.createContext(sandbox);
+        test.equal(sandbox.prop1, 'prop1');
+        test.equal(sandbox.prop2, 'prop2');
+        test.done();
+    },
+
     // Ensure that the correct properties exist on a wrapped sandbox.
     'test contextified object extra properties' : function (test) {
         var sandbox = Contextify({});
@@ -31,6 +42,15 @@ exports['basic tests'] = {
         test.done();
     },
 
+    'createContext should not modify the sandbox' : function (test) {
+        var sandbox = {};
+        Contextify.createContext(sandbox);
+        test.equal(sandbox.run, undefined);
+        test.equal(sandbox.getGlobal, undefined);
+        test.equal(sandbox.dispose, undefined);
+        test.done();
+    },
+
     // Passing undefined should create an empty context.
     'test undefined sandbox' : function (test) {
         // Should return an empty object.
@@ -65,6 +85,14 @@ exports['basic tests'] = {
         test.done();
     },
 
+    'test for "undefined" properties with createContext' : function (test) {
+        var sandbox = { x: undefined };
+        var context = Contextify.createContext(sandbox);
+        context.run("_x = x");
+        test.equal(sandbox._x, undefined);
+        test.done();
+    },
+
     'test for "undefined" variables' : function (test) {
         var sandbox = { };
         Contextify(sandbox);
@@ -88,6 +116,15 @@ exports['basic tests'] = {
         test.done();
     },
 
+    // Make sure run can be called on a context
+    'test run with createContext' : function (test) {
+        var sandbox = {};
+        var context = Contextify.createContext(sandbox);
+        context.run('var x = 3', "test.js");
+        test.equal(sandbox.x, 3);
+        test.done();
+    },
+
     // Make sure getters/setters on the sandbox object are used.
     'test accessors on sandbox' : function (test) {
         var sandbox = {};
@@ -192,6 +229,28 @@ exports['asynchronous script tests'] = {
             test.ok(sandbox.test2);
             test.done();
         }, 0);
+    },
+
+    // Asynchronous context script execution:
+    // Ensure that sandbox properties can be accessed as global variables.
+    'createContext: sandbox properties should be globals' : function (test) {
+        var sandbox = {
+            setTimeout : setTimeout,
+            prop1 : 'prop1',
+            prop2 : 'prop2'
+        };
+        var context = Contextify.createContext(sandbox);
+        context.run("setTimeout(function () {" +
+                    "test1 = (prop1 == 'prop1');" +
+                    "test2 = (prop2 == 'prop2');" +
+                    "}, 0)");
+        test.equal(sandbox.test1, undefined);
+        test.equal(sandbox.test2, undefined);
+        setTimeout(function () {
+            test.ok(sandbox.test1);
+            test.ok(sandbox.test2);
+            test.done();
+        }, 0);
     }
 };
 
@@ -301,7 +360,7 @@ exports['test global'] = {
         test.equal(global.x, 5);
         test.done();
     },
-    
+
     // Make sure global can be a receiver for getGlobal().
     'test global.getGlobal()' : function (test) {
         var global = Contextify().getGlobal();
@@ -436,7 +495,7 @@ exports['test exceptions'] = {
         }, 'Called dispose() twice.');
         test.done();
     },
-   
+
     'test run() after dispose()' : function (test) {
         var sandbox = Contextify();
         test.doesNotThrow(function () {
@@ -459,3 +518,36 @@ exports['test exceptions'] = {
         test.done();
     }
 };
+
+exports['test scripts'] = {
+    'test createScript()' : function (test) {
+        var script = Contextify.createScript('var x = 3', 'test.js');
+        test.equal(typeof script.runInContext, 'function');
+        test.done();
+    },
+
+    'test createScript() without code' : function (test) {
+        test.throws(function () {
+            Contextify.createScript();
+        });
+        test.throws(function () {
+            Contextify.createScript(true);
+        });
+        test.throws(function () {
+            Contextify.createScript(null);
+        });
+        test.throws(function () {
+            Contextify.createScript(1);
+        });
+        test.done();
+    },
+
+    'test runInContext' : function (test) {
+        var sandbox = {};
+        var script = Contextify.createScript('var x = 3', 'test.js');
+        var context = Contextify.createContext(sandbox);
+        script.runInContext(context);
+        test.equal(sandbox.x, 3);
+        test.done();
+    }
+};
diff --git a/wscript b/wscript
index 503ddd7..1f40583 100644
--- a/wscript
+++ b/wscript
@@ -2,7 +2,7 @@ import Options
 import os
 import sys
 
-VERSION = '0.1.2'
+VERSION = '0.1.6'
 
 def set_options(opt):
   opt.tool_options("compiler_cxx")
@@ -11,7 +11,6 @@ def configure(conf):
   conf.check_tool("compiler_cxx")
   conf.check_tool("node_addon")
   conf.env.set_variant("Release")
-  conf.env.append_value('CXXFLAGS', ['-O3'])
 
 def build(bld):
   obj = bld.new_task_gen("cxx", "shlib", "node_addon")

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/collab-maint/node-contextify.git



More information about the Pkg-javascript-commits mailing list