Permalink
Browse files

initial import 2

  • Loading branch information...
1 parent 92ba048 commit 80e5892f5295c809459341a6ce1c96dd37d38b37 @tokuhirom committed Sep 6, 2012
Showing with 200 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 .gitmodules
  3. +8 −0 README.md
  4. +55 −0 binding.gyp
  5. +3 −0 mrb-build.sh
  6. +70 −0 src/mrb.cc
  7. +59 −0 src/nodeutil.h
  8. +1 −0 vendor/mruby
View
@@ -0,0 +1 @@
+build/
View
@@ -0,0 +1,3 @@
+[submodule "vendor/mruby"]
+ path = vendor/mruby
+ url = git://github.com/mruby/mruby.git
View
@@ -0,0 +1,8 @@
+node-mruby
+==========
+
+preqreuirements
+---------------
+
+ * bison
+
View
@@ -0,0 +1,55 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'mruby',
+ 'sources': [
+ './src/mrb.cc',
+ ],
+ 'include_dirs': [
+ 'vendor/mruby/include/'
+ ],
+ 'dependencies': [
+ 'libmruby'
+ ],
+ 'libraries': [
+ ],
+ 'cflags!': [ '-fno-exceptions' ],
+ 'cflags_cc!': [ '-fno-exceptions' ],
+ 'conditions': [
+ ['OS=="mac"', {
+ 'xcode_settings': {
+ 'OTHER_LDFLAGS': [
+ ],
+ 'OTHER_CFLAGS': [
+ ]
+ },
+ }],
+ ],
+ 'cflags': [
+ ]
+ },
+ {
+ 'target_name': 'libmruby',
+ 'type': 'none',
+ 'actions': [
+ {
+ 'action_name': 'test',
+ 'inputs': [''],
+ 'outputs': [''],
+ 'conditions': [
+ ['OS=="win"', {
+ 'action': [
+ 'echo', 'test'
+ ]
+ }, {
+ 'action': [
+ # run libffi `make`
+ 'sh', 'mrb-build.sh'
+ ]
+ }]
+ ]
+ }
+ ]
+ }
+ ]
+}
View
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd vendor/mruby/
+make
View
@@ -0,0 +1,70 @@
+#include "nodeutil.h"
+
+#include "mruby.h"
+#include "mruby/array.h"
+#include "mruby/class.h"
+#include "mruby/compile.h"
+#include "mruby/hash.h"
+#include "mruby/khash.h"
+#include "mruby/proc.h"
+#include "mruby/string.h"
+#include "mruby/variable.h"
+
+using namespace v8;
+using namespace node;
+
+#define MRB_ (Unwrap<NodeMRuby>(args.This())->mrb_)
+#define CXT_ (Unwrap<NodeMRuby>(args.This())->cxt_)
+
+class NodeMRuby : ObjectWrap {
+public:
+ mrb_state* mrb_;
+ mrbc_context *cxt_;
+
+ static Persistent<FunctionTemplate> constructor_template;
+
+ static void Init(Handle<Object> target) {
+ Local<FunctionTemplate> t = FunctionTemplate::New(NodeMRuby::New);
+ constructor_template = Persistent<FunctionTemplate>::New(t);
+ constructor_template->SetClassName(String::NewSymbol("NodeMRubyObject"));
+
+ Local<ObjectTemplate> instance_template = constructor_template->InstanceTemplate();
+ instance_template->SetInternalFieldCount(1);
+
+ target->Set(String::NewSymbol("NodeMRubyObject"), constructor_template->GetFunction());
+ }
+
+ static Handle<Value> New(const Arguments& args) {
+ HandleScope scope;
+
+ if (!args.IsConstructCall())
+ return args.Callee()->NewInstance();
+
+ (new NodeMRuby())->Wrap(args.Holder());
+ return scope.Close(args.Holder());
+ }
+
+ NodeMRuby() {
+ mrb_ = mrb_open();
+ if (!mrb_) {
+ // TODO throw exception
+ }
+ cxt_ = mrbc_context_new(mrb_);
+ assert(cxt_);
+ }
+ ~NodeMRuby() {
+ mrb_close(mrb_);
+ }
+ static Handle<Value> parseString(const Arguments& args) {
+ HandleScope scope;
+ ARG_STR(0, src);
+ struct mrb_parser_state* st = mrb_parse_string(MRB_, *src, CXT_);
+ return scope.Close(Undefined());
+ }
+};
+
+#undef MRB_
+
+extern "C" void init(Handle<Object> target) {
+ NodeMRuby::Init(target);
+}
View
@@ -0,0 +1,59 @@
+#pragma once
+
+#define BUILDING_NODE_EXTENSION
+#include <node.h>
+#include <node_buffer.h>
+#include <v8.h>
+
+/* ******************************************************
+ * exception utilities
+ */
+#define THROW_TYPE_ERROR(str) \
+ ThrowException(Exception::TypeError(String::New(str)))
+
+/* ******************************************************
+ * Argument utilities.
+ */
+#define ARG_EXT(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsExternal()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be an external"))); \
+ Local<External> VAR = Local<External>::Cast(args[I]);
+
+/**
+ * ARG_STR(0, src);
+ *
+ * see http://blog.64p.org/entry/2012/09/02/101609
+ */
+#define ARG_STR(I, VAR) \
+ if (args.Length() <= (I)) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be a string"))); \
+ String::Utf8Value VAR(args[I]->ToString());
+
+#define ARG_OBJ(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsObject()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be a object"))); \
+ Local<Object> VAR = Local<Object>::Cast(args[I]);
+
+#define ARG_INT(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsInt32()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be an integer"))); \
+ int32_t VAR = args[I]->Int32Value();
+
+#define ARG_BUF(I, VAR) \
+ if (args.Length() <= (I) || !Buffer::HasInstance(args[I])) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be an Buffer"))); \
+ void * VAR = Buffer::Data(args[I]->ToObject());
+
+/* ******************************************************
+ * Class construction utilities
+ */
+#define SET_ENUM_VALUE(target, _value) \
+ target->Set(String::NewSymbol(#_value), \
+ Integer::New(_value), \
+ static_cast<PropertyAttribute>(ReadOnly|DontDelete))
+
Submodule mruby added at 7f3038

0 comments on commit 80e5892

Please sign in to comment.