Permalink
Browse files

Added node.js example

  • Loading branch information...
1 parent 10a4be5 commit 402d05db3d6aa1eedd1ebbb72076fad41efcd5ad @codeboost committed Jun 18, 2012
View
@@ -591,9 +591,9 @@ namespace bea{
v8::Handle<v8::External> ext;
v8::Handle<v8::Value> res;
- //if (args[0]->IsExternal())
- // ext = v8::Handle<v8::External>::Cast(args[0]);
- //else
+ if (args[0]->IsExternal())
+ ext = v8::Handle<v8::External>::Cast(args[0]);
+ else
{
assert(m_constructor != NULL && "Constructor not set!");
View
@@ -23,6 +23,17 @@ namespace hello{
Item(const std::string& name, int age): m_name(name), m_age(age){
}
+ Item(const Item& item){
+ m_name = item.m_name;
+ m_age = item.m_age;
+ }
+
+ Item duplicate(){
+ return Item(*this);
+ }
+
+ static std::string className();
+
virtual ~Item(){
std::cout << "~Item()";
}
@@ -31,6 +42,10 @@ namespace hello{
return m_name;
}
+ void setName(const std::string& name){
+ m_name = name;
+ }
+
std::string sayHello(const std::string &message){
//This calls a virtual function which can be overriden from Javascript
return m_name + greet() + message;
View
@@ -57,6 +57,12 @@ namespace helloJS {
hello::Item* fnRetVal = new helloJS::_D_Item(name);
return v8::External::New(fnRetVal);
}
+ //Item(const Item& other)
+ if (bea::Convert<hello::Item*>::Is(args[0])) {
+ hello::Item* other = bea::Convert<hello::Item*>::FromJS(args[0], 0);
+ hello::Item* fnRetVal = new helloJS::_D_Item(*other);
+ return v8::External::New(fnRetVal);
+ }
//Item()
if (args.Length() == 0) {
hello::Item* fnRetVal = new helloJS::_D_Item();
@@ -66,6 +72,15 @@ namespace helloJS {
METHOD_END();
}
+ v8::Handle<v8::Value> JItem::duplicate(const v8::Arguments& args) {
+ METHOD_BEGIN(0);
+ //Item duplicate()
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(args.This(), 0);
+ hello::Item* fnRetVal = new hello::Item(_this->duplicate());
+ return bea::Convert<hello::Item*>::ToJS(fnRetVal);
+ METHOD_END();
+ }
+
v8::Handle<v8::Value> JItem::name(const v8::Arguments& args) {
METHOD_BEGIN(0);
//std::string name()
@@ -75,6 +90,16 @@ namespace helloJS {
METHOD_END();
}
+ v8::Handle<v8::Value> JItem::setName(const v8::Arguments& args) {
+ METHOD_BEGIN(1);
+ //void setName(const std::string& name)
+ std::string name = bea::Convert<std::string>::FromJS(args[0], 0);
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(args.This(), 0);
+ _this->setName(name);
+ return args.This();
+ METHOD_END();
+ }
+
v8::Handle<v8::Value> JItem::sayHello(const v8::Arguments& args) {
METHOD_BEGIN(1);
//std::string sayHello(const std::string &message)
@@ -94,6 +119,14 @@ namespace helloJS {
METHOD_END();
}
+ v8::Handle<v8::Value> JItem::className(const v8::Arguments& args) {
+ METHOD_BEGIN(0);
+ //static std::string className()
+ std::string fnRetVal = _D_Item::className();
+ return bea::Convert<std::string>::ToJS(fnRetVal);
+ METHOD_END();
+ }
+
v8::Handle<v8::Value> JItem::__postAllocator(const v8::Arguments& args) {
METHOD_BEGIN(0);
//void __postAllocator()
@@ -109,9 +142,12 @@ namespace helloJS {
obj->setDestructor(__destructor);
//Exposed Methods
obj->setConstructor(__constructor);
+ obj->exposeMethod("duplicate", duplicate);
obj->exposeMethod("name", name);
+ obj->exposeMethod("setName", setName);
obj->exposeMethod("sayHello", sayHello);
obj->exposeMethod("greet", greet);
+ obj->exposeMethod("className", className);
obj->setPostAllocator(__postAllocator);
//Accessors
//Expose object to the Javascript
View
@@ -9,9 +9,12 @@ namespace helloJS {
static void __destructor(v8::Handle<v8::Value> value);
//Exported methods
static v8::Handle<v8::Value> __constructor(const v8::Arguments& args);
+ static v8::Handle<v8::Value> duplicate(const v8::Arguments& args);
static v8::Handle<v8::Value> name(const v8::Arguments& args);
+ static v8::Handle<v8::Value> setName(const v8::Arguments& args);
static v8::Handle<v8::Value> sayHello(const v8::Arguments& args);
static v8::Handle<v8::Value> greet(const v8::Arguments& args);
+ static v8::Handle<v8::Value> className(const v8::Arguments& args);
static v8::Handle<v8::Value> __postAllocator(const v8::Arguments& args);
public:
static void _InitJSObject(v8::Handle<v8::Object> target);
@@ -22,6 +25,7 @@ namespace helloJS {
_D_Item() : hello::Item(){}
_D_Item(const char* name) : hello::Item(name){}
_D_Item(const std::string& name, int age) : hello::Item(name, age){}
+ _D_Item(const Item& other) : hello::Item(other){}
//JS: These virtual functions will only be called from Javascript
inline std::string _d_greet() {
return hello::Item::greet();
@@ -18,6 +18,10 @@
Item()
Item(const char* name)
Item(const std::string& name, int age)
+ Item(const Item& other)
+ Item duplicate()
std::string name()
+ void setName(const std::string& name)
std::string sayHello(const std::string &message)
virtual std::string greet()
+ static std::string className()
View
@@ -8,6 +8,10 @@ void printLog(const char* msg){
std::cout << msg << std::endl;
}
+std::string hello::Item::className(){
+ return "Item";
+}
+
class ScriptController : public bea::BeaScript<helloJS::Project>{
View
@@ -1,6 +1,11 @@
_ = require('./underscore'); //Yes, you can do this!
log("In Javascript!");
+
+Item.prototype.clone = function(){
+ return new Item(this);
+}
+
var item = new Item();
log("item.name() should be (no name): " + item.name());
@@ -35,3 +40,23 @@ item.greet = function(){
msg = item.sayHello("How are you today?");
log(msg);
+
+item1 = item.duplicate()
+log('Duplicated name: ' + item1.name())
+
+item.setName("item name");
+item1.setName("item1 name1");
+
+log("Original name: " + item.name());
+log("Duplicated: " + item1.name());
+
+
+
+cloned = item.clone();
+cloned.setName("cloned name");
+log("Cloned item name: " + cloned.name());
+log("Original item name: " + item.name());
+
+
+//static function
+log("className = " + item.className());
@@ -0,0 +1,9 @@
+argv = ['/usr/local/bin/node-waf', 'configure', 'build']
+blddir = '/Users/florin/Sites/bea2/examples/node/build'
+commands = {'dist': 0, 'configure': True, 'distcheck': 0, 'install': 0, 'build': True, 'clean': 0, 'distclean': 0, 'check': 0, 'uninstall': 0}
+cwd = '/Users/florin/Sites/bea2/examples/node'
+environ = {'TERM_SESSION_ID': 'D46280D4-1062-4C8A-9A11-C42CC05C07B5', 'SSH_AUTH_SOCK': '/tmp/launch-ayEVl3/Listeners', 'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'TERM_PROGRAM_VERSION': '303.2', 'SHELL': '/bin/bash', 'LOGNAME': 'florin', 'USER': 'florin', 'HOME': '/Users/florin', 'PATH': '/Users/florin/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/usr/local/go/bin', 'DISPLAY': '/tmp/launch-7p39d3/org.x:0', 'TMPDIR': '/var/folders/2b/3ntd4jl124b4sdpy4hvnqqn00000gn/T/', 'TERM_PROGRAM': 'Apple_Terminal', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-color', 'Apple_PubSub_Socket_Render': '/tmp/launch-GeyKgW/Render', 'VERSIONER_PYTHON_VERSION': '2.7', 'SHLVL': '2', '__CF_USER_TEXT_ENCODING': '0x1F5:0:0', 'Apple_Ubiquity_Message': '/tmp/launch-pfLwZH/Apple_Ubiquity_Message', 'PWD': '/Users/florin/Sites/bea2/examples/node', '_': '/usr/local/bin/node-waf', 'COMMAND_MODE': 'unix2003'}
+files = []
+hash = 0
+options = {'compile_targets': None, 'force': False, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'destdir': '', 'keep': False, 'zones': '', 'blddir': '', 'prefix': '/usr/local/', 'jobs': 8, 'srcdir': '', 'check_cxx_compiler': 'g++'}
+srcdir = '/Users/florin/Sites/bea2/examples/node'
View
@@ -0,0 +1,22 @@
+#include <node.h>
+#include <v8.h>
+#include <bea.h>
+#include "helloJS.h"
+
+using namespace v8;
+
+
+void reportError(TryCatch& try_catch){
+ //if (m_logger)
+ //m_logger(*v8::String::Utf8Value(try_catch.StackTrace()));
+}
+
+bea::reportExceptionCb bea::Global::reportException = reportError;
+
+extern "C" {
+ static void init (Handle<Object> target) {
+ HandleScope scope;
+ helloJS::Project::expose(target);
+ }
+ NODE_MODULE(addon,init);
+}
Oops, something went wrong.

0 comments on commit 402d05d

Please sign in to comment.