Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updates and additional tests

  • Loading branch information...
commit a3d0d0e40edb681655be21e6c2bc7aae2770e431 1 parent 901faea
Samuel Shull authored
Showing with 207 additions and 178 deletions.
  1. +18 −16 src/node-proxy.cc
  2. +189 −162 test/test.js
34 src/node-proxy.cc
View
@@ -198,19 +198,19 @@ Handle<Value> NodeProxy::ValidateProxyHandler(Local<Object> handler) {
}
/**
+ * Causing Error
*
*
- */
Local<Value> NodeProxy::CorrectPropertyDescriptor(Local<Object> pd) {
- HandleScope scope;
+ //HandleScope scope;
- Local<Value> undef;
+ Local<Value> undef = Local<Value>::New(Undefined());
// pd->Set(NodeProxy::value,
// pd->Has(NodeProxy::value) ?
// pd->Get(NodeProxy::value) :
// undef);
-
+return Local<Value>::New(THREXC("CorrectPropertyDescriptor"));
pd->Set(NodeProxy::writable,
pd->Has(NodeProxy::writable) ?
pd->Get(NodeProxy::writable)->ToBoolean() :
@@ -819,7 +819,8 @@ Handle<Value> NodeProxy::DefineProperty(const Arguments& args) {
Local<String> name = args[1]->ToString();
Local<Object> handler = temp->ToObject();
- if (handler->GetHiddenValue(NodeProxy::sealed)->BooleanValue()) {
+ if (handler->GetHiddenValue(NodeProxy::sealed)->BooleanValue() ||
+ !handler->Has(NodeProxy::defineProperty)) {
return False();
}
@@ -833,17 +834,18 @@ Handle<Value> NodeProxy::DefineProperty(const Arguments& args) {
if (desc->Get(NodeProxy::configurable)->BooleanValue()) {
return Boolean::New(
- handler->Set(name,
- CorrectPropertyDescriptor(args[2]->ToObject())));
+ handler->Set(name, args[2]->ToObject()));
}
return False();
}
+
+
Local<Function> def = Local<Function>::Cast(
handler->Get(NodeProxy::defineProperty));
- Local<Value> argv[2] = {args[1],
- CorrectPropertyDescriptor(args[2]->ToObject())};
-
+
+ Local<Value> argv[2] = {args[1], args[2]->ToObject()};
+
return def->Call(obj, 2, argv)->ToBoolean();
}
@@ -876,7 +878,7 @@ Handle<Value> NodeProxy::DefineProperties(const Arguments& args) {
}
Local<Value> temp = obj->GetInternalField(0);
-
+
if (!temp.IsEmpty() && temp->IsObject()) {
Local<Object> props = args[1]->ToObject();
Local<Object> handler = temp->ToObject();
@@ -885,7 +887,6 @@ Handle<Value> NodeProxy::DefineProperties(const Arguments& args) {
return False();
}
- Local<Object> name;
bool extensible = handler->GetHiddenValue(
NodeProxy::extensible)->BooleanValue();
Local<Array> names = props->GetPropertyNames();
@@ -893,7 +894,7 @@ Handle<Value> NodeProxy::DefineProperties(const Arguments& args) {
if (!handler->GetHiddenValue(NodeProxy::trapping)->BooleanValue()) {
for (;i < l; ++i) {
- name = names->CloneElementAt(i);
+ Local<Object> name = names->CloneElementAt(i);
if (handler->Has(name->ToString()) &&
handler->Get(name->ToString())->IsObject()
@@ -924,10 +925,11 @@ Handle<Value> NodeProxy::DefineProperties(const Arguments& args) {
TryCatch firstTry;
for (;i < l; ++i) {
- name = names->CloneElementAt(i);
-
+ Local<Value> name = names->Get(i);
+
if (extensible || obj->Has(name->ToString())) {
- Local<Value> argv[2] = {name, props->Get(name->ToString())};
+ Local<Value> pd = props->Get(name->ToString());
+ Local<Value> argv[2] = {name, pd};
def->Call(obj, 2, argv);
if (firstTry.HasCaught()) {
351 test/test.js
View
@@ -1,171 +1,198 @@
-/*
+/*jslint forin: true, onevar: true, immed: true */
-Still working on this
-
-*/
-
-(function() {
+(function () {
require.paths.unshift(__dirname + "/../lib");
var sys = require('sys'),
assert = require('assert'),
- total_tests = 8,
- undef,
- called,
- Proxy = require("node-proxy"),
- createProxy = function(handlers) {
- called = "createProxy";
- return Proxy.create({
- getOwnPropertyDescriptor:function(name){
- called = "getOwnPropertyDescriptor";
- return handlers.hasOwnProperty(name) ? handlers[name] : undef;
- },
- getPropertyDescriptor:function(name){
- called = "getPropertyDescriptor";
- return name in handlers ? handlers[name] : undef;
- },
- defineProperty: defineProperty,
- getOwnPropertyNames:function(){
- called = "getOwnPropertyNames";
- return Object.getOwnPropertyNames(handlers);
- },
- "delete":function(name){
- called = "delete";
- var r = true;
- if (name in handlers) {
- r = (delete handlers[name]);
- }
- return r;
- },
- enumerate:function(){
- called = "enumerate";
- return Object.keys(handlers);
- },
- fix:function(){
- called = "fix";
- return handlers;
- },
- has:function(name){
- called = "has";
- //sys.puts("has called on: "+name);
- //sys.puts(name in handlers)
- return (name in handlers);
- },
- hasOwn:function(name){
- called = "hasOwn";
- return handlers.hasOwnProperty(name);
- },
- get:function(receiver, name){
- called = "get";
- //sys.puts(arguments.callee.caller)
- if (!(name in handlers)) {
- return undef;
- }
- return "get" in handlers[name] && typeof(handlers[name].get) == "function" ?
- handlers[name].get.call(receiver) :
- (handlers[name].value || undef);
- },
- set:function(receiver, name, val){
- called = "set";
- if (!(name in handlers)) {
- defineProperty.call(this, name, {
- configurable:true,
- writable:true,
- enumerable:true,
- value:val,
- get:function(){return val},
- set:function(v){val=v}
- });
- called = "set";
- return true;
- }
- if (!handlers[name].configurable) {
- return false;
- }
- if ("set" in handlers[name]) {
- handlers[name].set.call(receiver, val);
- }
-
- handlers[name].value = val;
- return true;
- },
- keys:function(){
- called = "keys";
- return Object.getOwnPropertyNames(handlers);
- },
- });
-
- function defineProperty(name, pd){
- called = "defineProperty";
- if (name in handlers && !handlers[name].configurable) {
- return null;
- }
- handlers[name] = pd;
- return null;
+ total_tests = 11,
+ undef,
+ called, p,
+ Proxy = require("node-proxy"),
+ createProxy = function (handlers) {
+ called = "createProxy";
+
+ function defineProperty(name, pd){
+ called = "defineProperty";
+ if (name in handlers && !handlers[name].configurable) {
+ return null;
+ }
+ handlers[name] = pd;
+ return null;
+ }
+
+ return Proxy.create({
+ getOwnPropertyDescriptor:function (name){
+ called = "getOwnPropertyDescriptor";
+ return handlers.hasOwnProperty(name) ? handlers[name] : undef;
+ },
+ getPropertyDescriptor:function (name){
+ called = "getPropertyDescriptor";
+ return name in handlers ? handlers[name] : undef;
+ },
+ defineProperty: defineProperty,
+ getOwnPropertyNames:function (){
+ called = "getOwnPropertyNames";
+ return Object.getOwnPropertyNames(handlers);
+ },
+ "delete":function (name){
+ called = "delete";
+ var r = true;
+ if (name in handlers) {
+ r = (delete handlers[name]);
+ }
+ return r;
+ },
+ enumerate:function (){
+ called = "enumerate";
+ return Object.keys(handlers);
+ },
+ fix:function (){
+ called = "fix";
+ return handlers;
+ },
+ has:function (name){
+ called = "has";
+ //sys.puts("has called on: "+name);
+ //sys.puts(name in handlers)
+ return (name in handlers);
+ },
+ hasOwn:function (name){
+ called = "hasOwn";
+ return handlers.hasOwnProperty(name);
+ },
+ get:function (receiver, name){
+ called = "get";
+ //sys.puts(arguments.callee.caller)
+ if (!(name in handlers)) {
+ return undef;
+ }
+ return "get" in handlers[name] && typeof(handlers[name].get) == "function" ?
+ handlers[name].get.call(receiver) :
+ (handlers[name].value || undef);
+ },
+ set:function (receiver, name, val){
+ called = "set";
+ if (!(name in handlers)) {
+ defineProperty.call(this, name, {
+ configurable:true,
+ writable:true,
+ enumerable:true,
+ value:val,
+ get:function (){return val;},
+ set:function (v){val=v;}
+ });
+ called = "set";
+ return true;
+ }
+ if (!handlers[name].configurable) {
+ return false;
+ }
+ if ("set" in handlers[name]) {
+ handlers[name].set.call(receiver, val);
+ }
+
+ handlers[name].value = val;
+ return true;
+ },
+ keys:function (){
+ called = "keys";
+ return Object.getOwnPropertyNames(handlers);
+ }
+ });
+ },
+ proxyTest,
+ firstValue = "firstProp",
+ names, count,
+ handlers = {
+ first: {
+ get:function (){return firstValue;},
+ set:function (val){firstValue = val;}
+ }
+ };
+
+ sys.puts("Running tests...");
+ sys.puts("Test 1 of " + total_tests + ": Creating proxy");
+ proxyTest = createProxy(handlers);
+ assert.equal(called, "createProxy", "createProxy was not the last method called");
+ assert.ok(typeof proxyTest == "object");
+
+ sys.puts("Test 2 of " + total_tests + ": has");
+ assert.ok("first" in proxyTest, "proxyTest does not have a property named 'first'");
+ //assert.equal(called, "has", "the has method was not the last method called");
+
+ sys.puts("Test 3 of " + total_tests + ": Accessing getter");
+ assert.equal(proxyTest.first, firstValue);
+ assert.equal(called, "get", "the get method was not the last method called");
+
+ sys.puts("Test 4 of " + total_tests + ": Accessing setter");
+ proxyTest.first = "changed";
+ assert.equal(called, "set", "the set method was not the last method called");
+ assert.equal(proxyTest.first, firstValue, "proxyTest.first != firstValue");
+
+ sys.puts("Test 5 of " + total_tests + ": set property");
+ proxyTest.second = "secondProp";
+ assert.equal(called, "set", "the set method was not the last method called");
+
+ sys.puts("Test 6 of " + total_tests + ": Iterate property names");
+ count = 0;
+ for (p in proxyTest){++count;}
+ assert.equal(count, 2, "there are not 2 properties on proxyTest");
+
+ sys.puts("Test 7 of " + total_tests + ": getOwnPropertyNames");
+ names = Object.getOwnPropertyNames(proxyTest);
+ assert.equal(called, "enumerate", "Object.getOwnPropertyNames did not invoke enumerate");
+ assert.ok(names instanceof Array);
+ assert.equal(names.length, 2, "2 property names were not returned");
+ assert.equal(names[0], "first", "The first property name is not 'first'");
+ assert.equal(names[1], "second", "The second property name is not 'second'");
+
+ sys.puts("Test 8 of " + total_tests + ": keys");
+ names = Object.keys(proxyTest);
+ assert.equal(called, "enumerate", "Object.keys did not invoke 'enumerate'");
+ assert.ok(names instanceof Array);
+ assert.equal(names.length, 2, "2 property names were not returned");
+ assert.equal(names[0], "first", "The first property name is not 'first'");
+ assert.equal(names[1], "second", "The second property name is not 'second'");
+
+ sys.puts("Test 9 of " + total_tests + ": delete");
+ assert.ok((delete proxyTest.second), "Delete the property 'second' from the proxy");
+ assert.equal(called, "delete", "the delete method was not the last method called");
+ assert.ok(!Object.prototype.hasOwnProperty.call(proxyTest, "second"), "proxyTest still hasOwnProperty the property 'second'");
+ assert.ok(!("second" in proxyTest), "proxyTest still has the property 'second'");
+
+ sys.puts("Test 10 of " + total_tests + ": defineProperty");
+ Proxy.defineProperty(proxyTest, 'third', {
+ get: function() {
+ return "third";
+ }
+ });
+ assert.equal(called, "defineProperty", "defineProperty was not called: "+called);
+ assert.ok("third" in proxyTest);
+ assert.equal(proxyTest.third, "third", "proxyTest.third != 'third'");
+
+ sys.puts("Test 11 of " + total_tests + ": defineProperties");
+ Proxy.defineProperties(proxyTest, {
+ fourth: {
+ get: function() {
+ return "fourth";
}
},
- protoProxy = function(){},
- proxyTest,
- firstValue = "firstProp",
- names, count,
- handlers = {
- first: {
- get:function(){return firstValue},
- set:function(val){firstValue = val}
+ fifth: {
+ get: function() {
+ return "fifth";
}
- };
-
- sys.puts("Running tests...");
- sys.puts("Test 1 of " + total_tests + ": Creating proxy");
- proxyTest = createProxy(handlers);
- assert.equal(called, "createProxy", "createProxy was not the last method called");
- assert.ok(typeof proxyTest == "object");
-
- sys.puts("Test 2 of " + total_tests + ": has");
- assert.ok("first" in proxyTest, "proxyTest does not have a property named 'first'");
- //assert.equal(called, "has", "the has method was not the last method called");
-
- sys.puts("Test 3 of " + total_tests + ": Accessing getter");
- assert.equal(proxyTest.first, firstValue);
- assert.equal(called, "get", "the get method was not the last method called");
-
- sys.puts("Test 4 of " + total_tests + ": Accessing setter");
- proxyTest.first = "changed";
- assert.equal(called, "set", "the set method was not the last method called");
- assert.equal(proxyTest.first, firstValue, "proxyTest.first != firstValue");
-
- sys.puts("Test 5 of " + total_tests + ": set property");
- proxyTest.second = "secondProp";
- assert.equal(called, "set", "the set method was not the last method called");
-
- sys.puts("Test 6 of " + total_tests + ": Iterate property names");
- count = 0;
- for (p in proxyTest){++count;}
- assert.equal(count, 2, "there are not 2 properties on proxyTest");
-
- sys.puts("Test 7 of " + total_tests + ": getOwnPropertyNames");
- names = Object.getOwnPropertyNames(proxyTest);
- assert.equal(called, "enumerate", "Object.getOwnPropertyNames did not invoke enumerate");
- assert.ok(names instanceof Array);
- assert.equal(names.length, 2, "2 property names were not returned");
- assert.equal(names[0], "first", "The first property name is not 'first'");
- assert.equal(names[1], "second", "The second property name is not 'second'");
-
- sys.puts("Test 8 of " + total_tests + ": keys");
- names = Object.keys(proxyTest);
- assert.equal(called, "enumerate", "Object.keys did not invoke 'enumerate'");
- assert.ok(names instanceof Array);
- assert.equal(names.length, 2, "2 property names were not returned");
- assert.equal(names[0], "first", "The first property name is not 'first'");
- assert.equal(names[1], "second", "The second property name is not 'second'");
-
- sys.puts("Test 9 of " + total_tests + ": delete");
- assert.ok((delete proxyTest.second), "Delete the property 'second' from the proxy");
- assert.equal(called, "delete", "the delete method was not the last method called");
- assert.ok(!Object.prototype.hasOwnProperty.call(proxyTest, "second"), "proxyTest still hasOwnProperty the property 'second'");
- assert.ok(!("second" in proxyTest), "proxyTest still has the property 'second'");
-
-
-
- process.exit(0);
+ }
+ });
+ assert.equal(called, "defineProperty", "defineProperty was not called: "+called);
+ assert.ok("fourth" in proxyTest);
+ assert.ok("fifth" in proxyTest);
+ assert.equal(proxyTest.fourth, "fourth", "proxyTest.fourth != 'fourth'");
+ assert.equal(proxyTest.fifth, "fifth", "proxyTest.fifth != 'fifth'");
+ assert.ok("fourth" in handlers, "'fourth' is not in handlers");
+ assert.ok("fifth" in handlers, "'fifth' is not in handlers");
+
+
+
+ process.exit(0);
}());
Please sign in to comment.
Something went wrong with that request. Please try again.