Permalink
Browse files

Added example project

  • Loading branch information...
1 parent 1148f9a commit 0ad3cd87d67f7226775ae25ab31520f0cd34d609 @codeboost committed Jun 13, 2012
View
@@ -1,2 +1,3 @@
node_modules
-tmp
+tmp
+*.o
View
@@ -7,6 +7,13 @@ Purpose
A short description of the API is found in this file.
+Building
+ You must have the boost libraries installed. Although it bothers me to have this dependency, it is the easiest way to do cross-platform filesystem operations (required by beascript, which loads scripts and resolves includes).
+ On MacOS X, use
+ brew install -v boost 2>&1
+
+ You must build v8.
+
Convert<T>
Specialize this with a custom type to define conversions from/to Javascript.
View
@@ -282,7 +282,7 @@ namespace bea{
expose();
- executeScript("./lib/loader.js");
+ executeScript("./loader.js");
CloneObject(m_context->Global(), globalSandbox);
@@ -1 +1,3 @@
-v8/*
+v8/*
+hello
+.sconsign.dblite
View
@@ -0,0 +1,16 @@
+Compiling on OSX:
+
+1. Get and compile V8: https://developers.google.com/v8/build
+2. Install boost
+ brew install -v boost 2>&1
+
+3. Install scons
+ brew install scons
+
+4. To build, type:
+ scons
+
+5. To run, type:
+ ./hello test.js
+
+
View
@@ -0,0 +1,41 @@
+#mkdir v8
+#copy v8.h to v8
+#copy libv8_base to v8
+#scons
+
+projectFiles = Split("""
+ main.cpp
+ helloJS.cpp
+ ../../cpp/beascript.cpp
+""")
+
+includePaths = Split("""
+ v8/include
+ ../../cpp
+""")
+
+libPaths = Split("""
+ v8
+""");
+
+if bool(ARGUMENTS.get("debug", 0)):
+ libs = Split("""
+ v8_base
+ v8_snapshot
+ boost_filesystem-mt
+ boost_system-mt
+ """)
+
+else:
+ libs = Split("""
+ v8_base
+ v8_snapshot
+ boost_filesystem-mt
+ boost_system-mt
+ """)
+
+Program('hello', projectFiles,
+ CPPPATH=includePaths,
+ LIBS=libs,
+ LIBPATH=libPaths)
+
View
@@ -0,0 +1,2 @@
+coffee ../../bea hellojs.bea -f
+scons
View
@@ -1,13 +0,0 @@
-{
- 'targets': [
- {
- 'target_name': 'hello',
- 'sources': [
- "main.cpp"
- ],
- 'dependencies': [
- 'v8'
- ],
- }
- ]
-}
View
@@ -0,0 +1,49 @@
+#ifndef HELLO_H
+#define HELLO_H
+
+#include <iostream>
+
+namespace hello{
+ //This is a mock class which we'll expose to javascript
+
+ class Item{
+ std::string m_name;
+ int m_age;
+ public:
+ //We can expose multiple constructor overloads
+ Item(){
+ m_name = "(no name)";
+ m_age = 0;
+ }
+
+ Item(const char* name): m_name(name){
+ m_age = 0;
+ }
+
+ Item(const std::string& name, int age): m_name(name), m_age(age){
+ }
+
+ virtual ~Item(){
+ std::cout << "~Item()";
+ }
+
+ std::string name(){
+ return m_name;
+ }
+
+ std::string sayHello(const std::string &message){
+ //This calls a virtual function which can be overriden from Javascript
+ return m_name + greet() + message;
+ }
+
+ //We can override functions in Javascript
+ virtual std::string greet(){
+ return " says hi: ";
+ }
+
+
+ };
+}//namespace hello
+
+
+#endif
View
@@ -0,0 +1,139 @@
+#include <bea.h>
+#include "helloJS.h"
+namespace bea {
+ template<> struct Convert<helloJS::_D_Item*> {
+ static bool Is(v8::Handle<v8::Value> v) {
+ return bea::ExposedClass<helloJS::_D_Item>::Is(v);
+ }
+
+ static helloJS::_D_Item* FromJS(v8::Handle<v8::Value> v, int nArg) {
+ return bea::ExposedClass<helloJS::_D_Item>::FromJS(v, nArg);
+ }
+
+ static v8::Handle<v8::Value> ToJS(helloJS::_D_Item* const& v) {
+ return bea::ExposedClass<helloJS::_D_Item>::ToJS(v);
+ }
+
+ };
+
+ template<> struct Convert<hello::Item*> {
+ static bool Is(v8::Handle<v8::Value> v) {
+ return bea::Convert<helloJS::_D_Item*>::Is(v);
+ }
+
+ static hello::Item* FromJS(v8::Handle<v8::Value> v, int nArg) {
+ return bea::Convert<helloJS::_D_Item*>::FromJS(v, nArg);
+ }
+
+ static v8::Handle<v8::Value> ToJS(hello::Item* const& v) {
+ return bea::Convert<helloJS::_D_Item*>::ToJS(static_cast<helloJS::_D_Item*>(v));
+ }
+
+ };
+
+}
+
+DECLARE_EXPOSED_CLASS(helloJS::_D_Item);
+namespace helloJS {
+ void JItem::__destructor(v8::Handle<v8::Value> value) {
+ DESTRUCTOR_BEGIN();
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(value, 0);
+ delete _this;
+ DESTRUCTOR_END();
+ }
+
+ v8::Handle<v8::Value> JItem::__constructor(const v8::Arguments& args) {
+ METHOD_BEGIN(0);
+ //Item(const std::string& name, int age)
+ if (bea::Convert<std::string>::Is(args[0]) && bea::Convert<int>::Is(args[1])) {
+ std::string name = bea::Convert<std::string>::FromJS(args[0], 0);
+ int age = bea::Convert<int>::FromJS(args[1], 1);
+ hello::Item* fnRetVal = new helloJS::_D_Item(name, age);
+ return v8::External::New(fnRetVal);
+ }
+ //Item(const char* name)
+ if (bea::Convert<bea::string>::Is(args[0])) {
+ bea::string name = bea::Convert<bea::string>::FromJS(args[0], 0);
+ hello::Item* fnRetVal = new helloJS::_D_Item(name);
+ return v8::External::New(fnRetVal);
+ }
+ //Item()
+ if (args.Length() == 0) {
+ hello::Item* fnRetVal = new helloJS::_D_Item();
+ return v8::External::New(fnRetVal);
+ }
+ return v8::ThrowException(v8::Exception::Error(v8::String::New(("Could not determine overload from supplied arguments"))));
+ METHOD_END();
+ }
+
+ v8::Handle<v8::Value> JItem::name(const v8::Arguments& args) {
+ METHOD_BEGIN(0);
+ //std::string name()
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(args.This(), 0);
+ std::string fnRetVal = _this->name();
+ return bea::Convert<std::string>::ToJS(fnRetVal);
+ METHOD_END();
+ }
+
+ v8::Handle<v8::Value> JItem::sayHello(const v8::Arguments& args) {
+ METHOD_BEGIN(1);
+ //std::string sayHello(const std::string &message)
+ std::string message = bea::Convert<std::string>::FromJS(args[0], 0);
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(args.This(), 0);
+ std::string fnRetVal = _this->sayHello(message);
+ return bea::Convert<std::string>::ToJS(fnRetVal);
+ METHOD_END();
+ }
+
+ v8::Handle<v8::Value> JItem::greet(const v8::Arguments& args) {
+ METHOD_BEGIN(0);
+ //virtual std::string greet()
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(args.This(), 0);
+ std::string fnRetVal = _this->greet();
+ return bea::Convert<std::string>::ToJS(fnRetVal);
+ METHOD_END();
+ }
+
+ v8::Handle<v8::Value> JItem::__postAllocator(const v8::Arguments& args) {
+ METHOD_BEGIN(0);
+ //void __postAllocator()
+ helloJS::_D_Item* _this = bea::Convert<helloJS::_D_Item*>::FromJS(args.This(), 0);
+ _this->bea_derived_setInstance(args.This());
+ return args.This();
+ METHOD_END();
+ }
+
+ void JItem::_InitJSObject(v8::Handle<v8::Object> target) {
+ bea::ExposedClass<helloJS::_D_Item>* obj = EXPOSE_CLASS(helloJS::_D_Item, "Item");
+ //Destructor
+ obj->setDestructor(__destructor);
+ //Exposed Methods
+ obj->setConstructor(__constructor);
+ obj->exposeMethod("name", name);
+ obj->exposeMethod("sayHello", sayHello);
+ obj->exposeMethod("greet", greet);
+ obj->setPostAllocator(__postAllocator);
+ //Accessors
+ //Expose object to the Javascript
+ obj->exposeTo(target);
+ }
+
+ std::string _D_Item::greet() {
+ v8::Locker v8locker;
+ v8::HandleScope v8scope; v8::Handle<v8::Value> v8retVal;
+ if (bea_derived_hasOverride("greet")) {
+ v8::Handle<v8::Value> v8args[1];
+ v8retVal = bea_derived_callJS("greet", 0, v8args);
+ }
+ if (v8retVal.IsEmpty()) return _d_greet();
+ return bea::Convert<std::string>::FromJS(v8retVal, 0);
+ }
+
+}
+
+namespace helloJS {
+ void Project::expose(v8::Handle<v8::Object> target) {
+ JItem::_InitJSObject(target);
+ }
+
+}
View
@@ -0,0 +1,44 @@
+#ifndef HELLOJS_H
+#define HELLOJS_H
+#include <v8.h>
+#include "hello.h"
+namespace helloJS {
+ class JItem {
+ protected:
+ //Destructor
+ 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> name(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> __postAllocator(const v8::Arguments& args);
+ public:
+ static void _InitJSObject(v8::Handle<v8::Object> target);
+ };
+
+ class _D_Item : public hello::Item, public bea::DerivedClass {
+ public:
+ _D_Item() : hello::Item(){}
+ _D_Item(const char* name) : hello::Item(name){}
+ _D_Item(const std::string& name, int age) : hello::Item(name, age){}
+ //JS: These virtual functions will only be called from Javascript
+ inline std::string _d_greet() {
+ return hello::Item::greet();
+ }
+
+ //Native: These virtual functions will only be called from native code
+ std::string greet();
+ };
+
+}
+
+namespace helloJS {
+ class Project {
+ public:
+ static void expose(v8::Handle<v8::Object> target);
+ };
+
+}
+
+#endif //#ifndef HELLOJS_H
View
@@ -0,0 +1,12 @@
+@project helloJS
+@targetNamespace helloJS
+
+@header
+ \#include <v8.h>
+ \#include "hello.h"
+
+@cpp
+ \#include <bea.h>
+ \#include "helloJS.h"
+
+@include "item.bea"
View
@@ -0,0 +1,15 @@
+@namespace hello
+ @type std::string
+ @class Item
+ @destructor
+ delete _this;
+
+ Item()
+ Item(const char* name)
+ Item(const std::string& name, int age)
+ std::string name()
+ std::string sayHello(const std::string &message)
+ virtual std::string greet()
+
+
+
Oops, something went wrong.

0 comments on commit 0ad3cd8

Please sign in to comment.