Permalink
Browse files

Initial Commit for Magic

Working library, possibly done?
  • Loading branch information...
0 parents commit 73dcfa164f518828697919588f2bd2b0d5879804 @aikar committed Apr 3, 2011
Showing with 398 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .npmignore
  3. +27 −0 LICENSE
  4. +31 −0 Makefile
  5. 0 README.markdown
  6. +47 −0 examples/magic.js
  7. +21 −0 index.js
  8. +41 −0 lib/magic.js
  9. +23 −0 package.json
  10. +29 −0 runTest.js
  11. +85 −0 src/magic.cc
  12. +17 −0 src/wscript
  13. +75 −0 test/magic.js
@@ -0,0 +1 @@
+lib/magicBindings.node
@@ -0,0 +1 @@
+lib/magicBindings.node
@@ -0,0 +1,27 @@
+==== Magic License ====
+The MIT License
+
+ Copyright (c) 2011 Daniel Ennis <aikar@aikar.co>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+====
+
+This license applies to all parts of Magic that are not externally
+maintained libraries.
@@ -0,0 +1,31 @@
+CWD = $(shell pwd -P)
+NODE_WAF ?= node-waf
+NODE_SRC_DIR ?= $(HOME)/src/node
+CFLAGS ?= -g -Wall
+CXXFLAGS ?= -g -Wall
+
+MODULE_LIB_DIR = $(CWD)/lib
+# We need to build position-independent code regardless of platform
+CFLAGS += -fPIC
+CXXFLAGS += -fPIC
+
+# These variables are respected by waf if we export them
+export CFLAGS CXXFLAGS
+
+all: magic
+
+#build libs then clean up - for NPM
+makelibs: magic cleanbuild
+
+# Build Magic
+magic:
+ cd src && \
+ $(NODE_WAF) configure build && \
+ cp ../build/default/magicBindings.node $(MODULE_LIB_DIR)
+
+cleanbuild:
+ rm -rf build src/.lock-wscript
+
+clean: cleanbuild
+ rm -f lib/whBindings.node
+
No changes.
@@ -0,0 +1,47 @@
+// Copyright (c) 2011 Daniel Ennis <aikar@aikar.co>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+var magic = require('../');
+var obj = magic(function(name) {
+ console.log('Request to get', name)
+}, function(name, val) {
+ console.log('Request to set', name, 'to', val);
+});
+var foo = obj.foo;
+obj.bar = 1;
+console.log(obj);
+function configChain() {
+ var requestChain = [];
+ function getter(name) {
+ requestChain.push(name);
+ return _chain();
+ }
+ function setter(name, value) {
+ getter(name);// add final key to chain
+ var key = requestChain.join('.');
+ console.log('Setting', key, 'to', value);
+ }
+ function _chain() {
+ return magic(getter, setter);
+ }
+ return _chain();
+}
+
+configChain().foo.bar.baz.hello.world = 42;
@@ -0,0 +1,21 @@
+// Copyright (c) 2011 Daniel Ennis <aikar@aikar.co>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+module.exports = require('./lib/magic');
@@ -0,0 +1,41 @@
+// Copyright (c) 2011 Daniel Ennis <aikar@aikar.co>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+var MagicObject = require('./magicBindings').MagicObject;
+function magic() {
+ var args = [].slice.call(arguments);
+ var cb1 = args.pop(), cb2;
+ var getter, setter, obj;
+ if (typeof cb1 == 'function') {
+ cb2 = args.pop();
+ if (typeof cb2 == 'function') {
+ setter = cb1;
+ getter = cb2;
+ } else {
+ getter = cb1;
+ setter = function(x,y){ return y; };
+ }
+ return new MagicObject(getter, setter);
+ }
+ return null;
+}
+magic.MagicObject = MagicObject;
+
+module.exports = magic;
@@ -0,0 +1,23 @@
+{
+ "name": "magic",
+ "description": "Magic Method/getter/setters for Node.JS",
+ "version": "1.0.0",
+ "homepage": "http://aikar.co/magic",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/aikar/magic.git"
+ },
+ "author": "Aikar <aikar@aikar.co> (http://aikar.co)",
+ "main": "lib/magic.js",
+ "directories": {
+ "lib": "lib"
+ },
+ "scripts": {
+ "test": "runTests.js",
+ "preinstall": "make makelibs",
+ "preuninstall": "make clean"
+ },
+ "engines": {
+ "node": ">=4 || 5"
+ }
+}
@@ -0,0 +1,29 @@
+#!/usr/bin/env node
+// Copyright (c) 2011 Daniel Ennis <aikar@aikar.co>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+var fs = require('fs');
+process.chdir(__dirname + '/test');
+if (process.argv.length <= 2) {
+ // Add all tests to be ran.
+ process.argv = process.argv.concat(fs.readdirSync('.'));
+}
+process.argv.push('--spec');
+require('vows/bin/vows'); // load the CLI script.
@@ -0,0 +1,85 @@
+// Copyright (c) 2011 Daniel Ennis <aikar@aikar.co>
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#include <v8.h>
+#include <node.h>
+
+using namespace v8;
+using namespace node;
+
+static Persistent<FunctionTemplate> object_temp;
+
+Handle<Value> magic_Getter(Local<String> name, const AccessorInfo& info) {
+ Handle<Object> object = info.Holder();
+ Handle<Function> getter = Handle<Function>::Cast(object->GetInternalField(0));
+
+ Handle<Value> args[1] = {name};
+
+ return getter->Call(object, 1, args);
+}
+Handle<Value> magic_Setter(Local<String> name, Local<Value> value, const AccessorInfo& info) {
+ Handle<Object> object = info.Holder();
+ Handle<Function> setter = Handle<Function>::Cast(object->GetInternalField(1));
+
+ Handle<Value> args[2] = {name, value};
+
+ return setter->Call(object, 2, args);
+}
+
+Handle<Value> magic_Getter(uint32_t name, const AccessorInfo& info) {
+ Handle<Object> object = info.Holder();
+ Handle<Function> getter = Handle<Function>::Cast(object->GetInternalField(0));
+
+ Handle<Value> args[1] = {Integer::New(name)};
+
+ return getter->Call(object, 1, args);
+}
+Handle<Value> magic_Setter(uint32_t name, Local<Value> value, const AccessorInfo& info) {
+ Handle<Object> object = info.Holder();
+ Handle<Function> setter = Handle<Function>::Cast(object->GetInternalField(1));
+
+ Handle<Value> args[2] = {Integer::New(name), value};
+
+ return setter->Call(object, 2, args);
+}
+/**
+ * Sets magic getter/setters on object.
+ */
+static Handle<Value> node_magic(const Arguments &args) {
+ Local<Object> object = args.This();
+ object->SetInternalField(0, args[0]);
+ object->SetInternalField(1, args[1]);
+ return args.This();
+}
+
+/**
+ * Exports the functions
+ */
+extern "C" void init(Handle<Object> target) {
+ HandleScope scope;
+ Local<FunctionTemplate> t = FunctionTemplate::New(node_magic);
+ object_temp = Persistent<FunctionTemplate>::New(t);
+ object_temp->InstanceTemplate()->SetInternalFieldCount(2);
+ object_temp->SetClassName(String::NewSymbol("MagicObject"));
+ object_temp->InstanceTemplate()->SetNamedPropertyHandler(magic_Getter, magic_Setter);
+ object_temp->InstanceTemplate()->SetIndexedPropertyHandler(magic_Getter, magic_Setter);
+
+ target->Set(String::NewSymbol("MagicObject"), object_temp->GetFunction());
+}
@@ -0,0 +1,17 @@
+srcdir = '.'
+blddir = '../build'
+VERSION = '1.0.0'
+
+def set_options(ctx):
+ ctx.tool_options('compiler_cxx')
+
+def configure(ctx):
+ ctx.check_tool('compiler_cxx')
+ ctx.check_tool('node_addon')
+
+def build(ctx):
+ t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
+ t.target = 'magicBindings'
+ t.source = 'magic.cc'
+
+# vim:ts=4 sw=4 et filetype=python
Oops, something went wrong.

0 comments on commit 73dcfa1

Please sign in to comment.