Permalink
Browse files

Add initial jsv8 module implementation

This does not work yet. Working on some things which are not included in this version (e.g., move js-inheritance code from js to c++)
  • Loading branch information...
1 parent 5751659 commit c02f6fa882479ce757306fa93a40c6e547eccc0e @oliver---- oliver---- committed Mar 29, 2011
View
@@ -0,0 +1,4 @@
+build
+third_party/swig/swig
+third_party/v8/v8
+
View
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8)
project(swig-js)
+set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "")
+
# Options:
# set(TARGET_ARCHITECTURE "ia32" CACHE STRING "arm, ia32, x64, mips")
@@ -53,4 +55,4 @@ add_subdirectory(third_party)
add_subdirectory(src/swig)
-# add_subdirectory(src/draft)
+add_subdirectory(src/Examples/jsv8)
@@ -0,0 +1,9 @@
+include_directories(${V8_DIR}/include)
+
+if(WIN32)
+ list(APPEND V8_LINK_LIBS "WINMM.LIB" ${V8_LIBS})
+else()
+ list(APPEND V8_LINK_LIBS ${V8_LIBS})
+endif()
+
+add_subdirectory(class)
@@ -0,0 +1,32 @@
+include_directories(${CMAKE_CURRENT_SOURCE_DIR})
+
+add_custom_command(
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/example_wrap.cxx
+ COMMAND ${SWIG_JS_EXECUTABLE} -jsv8 -c++ -debug-top 4 -o ${CMAKE_CURRENT_BINARY_DIR}/example_wrap.cxx -outdir ${CMAKE_CURRENT_BINARY_DIR} example.i > ${CMAKE_CURRENT_BINARY_DIR}/output.txt
+ COMMENT "${SWIG_JS_EXECUTABLE} -jsv8 -c++ -debug-top 4 -o ${CMAKE_CURRENT_BINARY_DIR}/example_wrap.cxx -outdir ${CMAKE_CURRENT_BINARY_DIR} example.i > ${CMAKE_CURRENT_BINARY_DIR}/output.txt"
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_executable(jsv8_class
+ example.cxx
+ example.h
+ main.cxx
+ ${CMAKE_CURRENT_BINARY_DIR}/example_wrap.cxx
+)
+
+add_dependencies(jsv8_class v8 swig)
+target_link_libraries(jsv8_class ${V8_LINK_LIBS})
+set_target_properties(jsv8_class PROPERTIES FOLDER "examples/v8")
+
+if (NOT EXISTS ${PROJECT_BINARY_DIR}/bin)
+ execute_process(
+ COMMAND ${CMAKE_COMMAND} -E make_directory "${PROJECT_BINARY_DIR}/bin"
+ )
+endif()
+
+get_target_property(class_exe jsv8_class LOCATION)
+add_custom_command(
+ TARGET jsv8_class
+ POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy ${class_exe} ${PROJECT_BINARY_DIR}/bin
+)
@@ -0,0 +1,28 @@
+/* File : example.c */
+
+#include "example.h"
+#define M_PI 3.14159265358979323846
+
+/* Move the shape to a new location */
+void Shape::move(double dx, double dy) {
+ x += dx;
+ y += dy;
+}
+
+int Shape::nshapes = 0;
+
+double Circle::area(void) {
+ return M_PI*radius*radius;
+}
+
+double Circle::perimeter(void) {
+ return 2*M_PI*radius;
+}
+
+double Square::area(void) {
+ return width*width;
+}
+
+double Square::perimeter(void) {
+ return 4*width;
+}
@@ -0,0 +1,39 @@
+/* File : example.h */
+
+class Shape {
+public:
+ Shape() {
+ nshapes++;
+ }
+ virtual ~Shape() {
+ nshapes--;
+ };
+ double x, y;
+ void move(double dx, double dy);
+ virtual double area(void) = 0;
+ virtual double perimeter(void) = 0;
+ static int nshapes;
+};
+
+class Circle : public Shape {
+private:
+ double radius;
+public:
+ Circle(double r) : radius(r) { };
+ virtual double area(void);
+ virtual double perimeter(void);
+};
+
+class Square : public Shape {
+private:
+ double width;
+public:
+ Square(double w) : width(w) { };
+ virtual double area(void);
+ virtual double perimeter(void);
+};
+
+
+
+
+
@@ -0,0 +1,10 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
@@ -0,0 +1,35 @@
+#include <v8.h>
+
+#include <iostream>
+#include <string>
+
+extern void Init_example(void);
+
+using namespace v8;
+
+void test1() {
+ HandleScope handle_scope;
+
+ Persistent<v8::Context> context = v8::Context::New();
+ v8::Context::Scope context_scope(context);
+
+ Init_example();
+
+ // Handle<String> source = String::New("var bla = new Bla(); bla.bla(); bla.blupp(2);");
+// Handle<String> source = String::New("var circle = new Circle(2.0); log('Hallo Welt!'); log(circle.area());");
+ Handle<String> source = String::New("var circle = new Circle(2.0); circle.x = 1.2;");
+ // Compile the source code.
+ Handle<Script> script = Script::Compile(source);
+
+ // Run the script to get the result.
+ Handle<Value> result = script->Run();
+
+ context.Dispose();
+}
+
+
+int main(int argc, char* argv[]) {
+
+ test1();
+ return 0;
+}
@@ -7,14 +7,23 @@ get_directory_property(SWIG_INCLUDES DIRECTORY ${SWIG_SOURCE_DIR} INCLUDE_DIRECT
include_directories(${SWIG_INCLUDES})
-add_library(swig_v8 v8/jsv8.cxx)
+add_executable(swig swigmain.cxx jsv8.cxx)
-#
-# swig.exe
-#
+target_link_libraries(swig
+ swig_cparse swig_preprocessor swig_doh swiglib swig_modules)
+
+get_target_property(SWIG_EXE swig LOCATION)
+get_filename_component(SWIG_EXE_NAME ${SWIG_EXE} NAME)
+
+add_custom_command(TARGET swig
+ POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy ${SWIG_EXE} ${PROJECT_BINARY_DIR}
+)
-add_executable(swig_exe swigmain.cxx)
+add_custom_command(TARGET swig
+ POST_BUILD
+ COMMAND ${CMAKE_COMMAND} -E copy_directory ${SWIG_SOURCE_DIR}/swig/Lib ${PROJECT_BINARY_DIR}/Lib
+ COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Lib ${PROJECT_BINARY_DIR}/Lib
+)
-target_link_libraries(swig_exe
- swig_cparse swig_preprocessor swig_doh swiglib swig_modules
- swig_v8)
+set(SWIG_JS_EXECUTABLE ${PROJECT_BINARY_DIR}/${SWIG_EXE_NAME} CACHE INTERNAL "" FORCE)
@@ -0,0 +1,59 @@
+/* ------------------------------------------------------------
+ * jsv8.swg
+ *
+ * JavaScript V8 configuration module.
+ * ------------------------------------------------------------ */
+
+/* ------------------------------------------------------------
+ * Inner macros
+ * ------------------------------------------------------------ */
+%include <jsv8macros.swg>
+
+/* ------------------------------------------------------------
+ * The runtime part
+ * ------------------------------------------------------------ */
+%include <jsv8runtime.swg>
+
+/* ------------------------------------------------------------
+ * Special user directives
+ * ------------------------------------------------------------ */
+%include <jsv8userdir.swg>
+
+/* ------------------------------------------------------------
+ * Typemap specializations
+ * ------------------------------------------------------------ */
+%include <jsv8typemaps.swg>
+
+/* ------------------------------------------------------------
+ * Overloaded operator support
+ * ------------------------------------------------------------ */
+%include <jsv8opers.swg>
+
+/* ------------------------------------------------------------
+ * Warnings for jsv8 keywords
+ * ------------------------------------------------------------ */
+%include <jsv8kw.swg>
+
+/* ------------------------------------------------------------
+ * Documentation for common jsv8 methods
+ * ------------------------------------------------------------ */
+//%include <jsv8autodoc.swg>
+
+/* ------------------------------------------------------------
+ * The jsv8 initialization function
+ * ------------------------------------------------------------ */
+%include <jsv8init.swg>
+
+/* Pointers, references */
+%typemap(out,noblock=1) SWIGTYPE *, SWIGTYPE &, SWIGTYPE[] {
+ %set_output(v8_set_new_instance(args, %as_voidptr($1)));
+}
+
+%typemap(out, noblock=1) SWIGTYPE *const& {
+ %set_output(v8_set_new_instance(args, %as_voidptr(*$1)));
+}
+
+/* Return by value */
+%typemap(out, noblock=1) SWIGTYPE {
+ %set_output(v8_set_new_instance(args, %new_copy($1, $ltype)));
+}
@@ -0,0 +1,55 @@
+ /* -----------------------------------------------------------------------------
+ * jsv8api.swg
+ *
+ * Defines some API methods for internal use in V8 extension.
+ *
+ * ----------------------------------------------------------------------------- */
+#define JSV8_CLASS v8::Persistent<v8::FunctionTemplate>
+
+v8::Persistent<v8::FunctionTemplate> v8_create_class_template(const char* symbol)
+{
+ v8::Local<v8::FunctionTemplate> class_templ = v8::FunctionTemplate::New();
+ class_templ->SetClassName(v8::String::NewSymbol(symbol));
+
+ v8::Handle<v8::ObjectTemplate> inst_templ = class_templ->InstanceTemplate();
+ inst_templ->SetInternalFieldCount(1);
+
+ return v8::Persistent<v8::FunctionTemplate>::New(class_templ);
+}
+
+void v8_set_allocate_handler(v8::Persistent<v8::FunctionTemplate> class_templ, v8::InvocationCallback allocHandler)
+{
+ class_templ->SetCallHandler(allocHandler);
+}
+
+v8::Handle<v8::Value> v8_set_new_instance(const v8::Arguments& args, void* data) {
+ v8::HandleScope scope;
+
+ v8::Handle<v8::Object> self = args.Holder();
+ self->SetInternalField(0, v8::External::New(data));
+
+ return self;
+}
+
+void v8_add_class_method(v8::Handle<v8::FunctionTemplate> class_templ, const char* symbol, v8::InvocationCallback _func)
+{
+ v8::Handle<v8::ObjectTemplate> proto_templ = class_templ->PrototypeTemplate();
+ proto_templ->Set(v8::String::NewSymbol(symbol), v8::FunctionTemplate::New(_func));
+}
+
+void v8_add_class_member_getters_setters(v8::Handle<v8::FunctionTemplate> class_templ, const char* varname, v8::AccessorGetter getter, v8::AccessorSetter setter) {
+ v8::Handle<v8::ObjectTemplate> proto_templ = class_templ->InstanceTemplate();
+ proto_templ->SetAccessor(v8::String::New(varname), getter, setter);
+}
+
+template <class T>
+T* v8_unwrap_this_pointer (v8::Handle<v8::Object> handle) {
+ assert(!handle.IsEmpty());
+ assert(handle->InternalFieldCount() > 0);
+ v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(handle->GetInternalField(0));
+ return static_cast<T*>(wrap->Value());
+}
+
+/* -----------------------------------------------------------------------------
+ * end of jsv8api.swg
+ * ----------------------------------------------------------------------------- */
@@ -0,0 +1,31 @@
+/* -----------------------------------------------------------------------------
+ * jsv8errors.swg
+ * ----------------------------------------------------------------------------- */
+
+// TODO: map Swig errors to v8 errors
+SWIGINTERN VALUE
+SWIG_Js_ErrorType(int SWIG_code) {
+ VALUE type;
+ switch (SWIG_code) {
+ case SWIG_MemoryError:
+ case SWIG_IOError:
+ case SWIG_RuntimeError:
+ case SWIG_IndexError:
+ case SWIG_TypeError:
+ case SWIG_DivisionByZero:
+ case SWIG_OverflowError:
+ case SWIG_SyntaxError:
+ case SWIG_ValueError:
+ case SWIG_SystemError:
+ case SWIG_AttributeError:
+ case SWIG_NullReferenceError:
+ case SWIG_UnknownError:
+ default:
+ type = v8::Undefined();
+ }
+ return type;
+}
+
+/* -----------------------------------------------------------------------------
+ * end of jsv8errors.swg
+ * ----------------------------------------------------------------------------- */
@@ -0,0 +1,14 @@
+/* -----------------------------------------------------------------------------
+ * jsv8head.swg
+ * ----------------------------------------------------------------------------- */
+
+#include <v8.h>
+#include <assert.h>
+
+#define VALUE v8::Handle<v8::Value>
+#define JSV8_UNDEFINED v8::Undefined()
+#define JSV8_VOID JSV8_UNDEFINED
+
+/* -----------------------------------------------------------------------------
+ * end of jsv8head.swg
+ * ----------------------------------------------------------------------------- */
@@ -0,0 +1,10 @@
+/* -----------------------------------------------------------------------------
+ * jsv8init.swg
+ * ----------------------------------------------------------------------------- */
+
+// swiginit.swg shall be inserted before Init_<module> function
+%insert(initbeforefunc) "swiginit.swg"
+
+/* -----------------------------------------------------------------------------
+ * end of jsv8init.swg
+ * ----------------------------------------------------------------------------- */
Oops, something went wrong.

0 comments on commit c02f6fa

Please sign in to comment.