Skip to content

Commit

Permalink
Sync with narwhal-jsc: binary, file, io, os, and system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Robinson committed Oct 7, 2009
1 parent ed21797 commit b32de1e
Show file tree
Hide file tree
Showing 16 changed files with 1,356 additions and 70 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -6,6 +6,7 @@ MODULES =$(patsubst %.cc,%.dylib,$(patsubst src/%,lib/%,$(wildcard src/*.cc)))

# change to libv8_g.dylib for debug version:
V8NAME =libv8.dylib
#V8NAME =libv8_g.dylib
V8 =v8/$(V8NAME)

all: $(V8) bin/narwhal-v8 modules
Expand Down
8 changes: 4 additions & 4 deletions include/binary-engine.h
@@ -1,11 +1,11 @@
#ifndef __BINARY_ENGINE__
#define __BINARY_ENGINE__

typedef struct __BytesPrivate BytesPrivate;

struct __BytesPrivate {
typedef struct __BytesPrivate {
char *buffer;
size_t length;
};
} BytesPrivate;

//JSObjectRef Bytes_new(JSContextRef _context, JSValueRef *_exception, char* buffer, int length);

#endif
23 changes: 23 additions & 0 deletions include/io-engine.h
@@ -0,0 +1,23 @@
#ifndef __IO_ENGINE__
#define __IO_ENGINE__

#include <iconv.h>

typedef struct __IOPrivate {
int input;
int output;
} IOPrivate;

typedef struct __TextInputStreamPrivate {
int input;
iconv_t cd;

char *inBuffer;
size_t inBufferSize;
size_t inBufferUsed;
} TextInputStreamPrivate;

//extern "C" JSClassRef IO_class(JSContextRef _context);
//extern "C" JSClassRef TextInputStream_class(JSContextRef _context);

#endif
7 changes: 4 additions & 3 deletions include/k7macros.h
Expand Up @@ -119,7 +119,8 @@ using namespace v8;
// type var = reinterpret_cast<type>(Handle<External>::Cast(_intfld)->Value());

#define GET_INTERNAL(type, name, object) \
type name = reinterpret_cast<type>(Handle<External>::Cast(object->GetInternalField(0))->Value())
type name = reinterpret_cast<type>(Handle<External>::Cast(object->GetInternalField(0))->Value());

#define SET_INTERNAL(object, data) \
object->SetInternalField(0, External::New((void*)data))

Expand Down Expand Up @@ -177,7 +178,7 @@ v8::Handle<Object> name(__VA_ARGS__) { \
// CLASS MACROS
//
// ----------------------------------------------------------------------------

/*
#define CLASS(name)\
{ \
v8::Handle<v8::String> __class_name__ = v8::String::New(name); \
Expand All @@ -191,7 +192,7 @@ v8::Handle<Object> name(__VA_ARGS__) { \
#define INTERNAL_FIELDS(i) __object__->SetInternalFieldCount(i);
#define HAS_INTERNAL __object__->SetInternalFieldCount(1);
#define END_CLASS __module__->Set(__class_name__,__class__->GetFunction(),v8::PropertyAttribute(v8::ReadOnly|v8::DontDelete));}

*/
// ----------------------------------------------------------------------------
//
// MODULE MACROS
Expand Down
106 changes: 91 additions & 15 deletions include/narwhal.h
Expand Up @@ -4,34 +4,45 @@
#include <v8.h>
#include "k7macros.h"

#include <stdlib.h>
#include <string.h>


// declare the module's 5 free variables and the C versions of "print" and "require" so they're available to the whole file
/*extern v8::Persistent<v8::Function> Require;
///*
extern v8::Persistent<v8::Function> Require;
extern v8::Persistent<v8::Object> Exports;
extern v8::Persistent<v8::Object> Module;
extern v8::Persistent<v8::Object> System;
extern v8::Persistent<v8::Function> Print;

extern void print(const char * string);
extern v8::Handle<v8::Object> require(const char * id);
*/
//*/

// MACROS:

#define NARWHAL_V8 1

//#define DEBUG_ON
#ifdef DEBUG_ON
#define DEBUG(...) LOG(__VA_ARGS__)
#define THROW_DEBUG " (%s:%d)\n"
#else
#define NOTRACE
#define DEBUG(...)
#define THROW_DEBUG
#endif

// "#define NOTRACE" at the top of files you don't want to enable tracing on
#ifdef NOTRACE
#define TRACE(...)
#else
#define TRACE(...) fprintf(stderr, __VA_ARGS__);
#define TRACE(...) LOG(__VA_ARGS__);
#endif

#define DEBUG(...)
//#define DEBUG(...) fprintf(stderr, __VA_ARGS__);

//#define THROW_DEBUG
#define THROW_DEBUG " (%s:%d)\n"
#define ERROR(...) LOG(__VA_ARGS__)
#define LOG(...) fprintf(stderr, __VA_ARGS__); fflush(stderr);

#define THROW(format, ...) \
{ char msg[1024]; snprintf(msg, 1024, format THROW_DEBUG, ##__VA_ARGS__, __FILE__, __LINE__); \
Expand All @@ -45,14 +56,22 @@ extern v8::Handle<v8::Object> require(const char * id);
#define THIS (args.This())

#define ARGV(n) (args[n])
#define IS_STRING(value) (value)->IsString()

#define ARGN_UTF8_CAST(variable, index) \
if (index >= ARGC) THROW("Argument %d must be a string.", index) \
_tmpStr = v8::Handle<v8::String>::Cast(ARGV(index)); \
#define IS_STRING(value) (value)->IsString()
#define IS_NUMBER(value) (value)->IsNumber()
#define IS_NULL(value) (value->IsNull())

#define TO_STRING(value) (value->ToString())

#define GET_UTF8(variable, value) \
_tmpStr = value->ToString(); \
_tmpSz = _tmpStr->Utf8Length(); \
char variable[_tmpSz+1]; \
_tmpStr->WriteUtf8(variable);

#define ARGN_UTF8_CAST(variable, index) \
if (index >= ARGC) THROW("Argument %d must be a string.", index) \
GET_UTF8(variable, ARGV(index));

#define ARGN_UTF8(variable, index) \
if (index >= ARGC && !IS_STRING(ARGV(index))) THROW("Argument %d must be a string.", index) \
Expand All @@ -61,10 +80,18 @@ extern v8::Handle<v8::Object> require(const char * id);
#define ARGN_STR(variable, index) \
if (index >= ARGC && !IS_STRING(ARGV(index))) THROW("Argument %d must be a string.", index) \
v8::Handle<v8::String> variable = v8::Handle<v8::String>::Cast(ARGV(index));


#define ARGN_DOUBLE(variable, index) \
if (index >= ARGC && !IS_NUMBER(ARGV(index))) THROW("Argument %d must be a number.", index) \
double variable = (args[index]->NumberValue())

#define ARGN_STR_OR_NULL(variable, index) \
NWValue variable = ((index < ARGC) && IS_STRING(ARGV(index))) ? ARGV(index) : JS_null;

#define ARG_UTF8(variable) 0; ARGN_UTF8(variable, _argn); _argn++; 0
#define ARG_UTF8_CAST(variable) 0; ARGN_UTF8_CAST(variable, _argn); _argn++; 0
#define ARG_STR(variable) 0; ARGN_STR(variable, _argn); _argn++; 0
#define ARG_DOUBLE(variable) 0; ARGN_DOUBLE(variable, _argn); _argn++; 0

#define FUNCTION(f,...) \
v8::Handle<v8::Value> f(const v8::Arguments& args) { \
Expand Down Expand Up @@ -116,20 +143,69 @@ extern v8::Handle<v8::Object> require(const char * id);
#define DESTRUCTOR(f) void f(Persistent<Value> value, void *) \
{ Local<Object> object = value->ToObject(); \

#define JSString v8::Handle<v8::Value>
#define NWValue v8::Handle<v8::Value>
#define NWObject v8::Handle<v8::Object>
#define NWString v8::Handle<v8::String>
#define NWDate v8::Handle<v8::Date>
#define NWArray v8::Handle<v8::Array>

typedef uint16_t NWChar;

#define ARGS_ARRAY(name, ...) Handle<Value> name[] = { __VA_ARGS__ };

#define CALL_AS_FUNCTION(object, thisObject, argc, argv) \
Function::Cast(*object)->Call(thisObject, argc, argv)

#define CALL_AS_CONSTRUCTOR(object, argc, argv) \
Function::Cast(*object)->NewInstance(argc, argv)

#define JS_str_utf8(str, len) v8::String::New(str, len)
#define JS_str_utf16(str, len) v8::String::New((uint16_t*)str, (len)/sizeof(uint16_t))

#define GET_UTF16(string, buffer, length) _GET_UTF16(string, (uint16_t **)buffer, length)
int _GET_UTF16(v8::Handle<v8::String> string, uint16_t **buffer, size_t *length) {
v8::String::Value value(string);
*buffer = *value;
*length = value.length() * sizeof(uint16_t);
//*buffer = *value;

*buffer = (uint16_t *)malloc(*length);
memcpy(*buffer, *value, *length);

return 1;
}

NWValue JS_date(int ms) {
return v8::Date::New(ms);
}

NWObject JS_array(size_t argc, NWValue argv[]) {
return v8::Array::New(argc >= 0 ? argc : 0);
}

#define CALL(f, ...) f(__VA_ARGS__)

#define SET_VALUE(object,s,v) object->Set(JS_str(s), v)
#define GET_VALUE(object,s) object->Get(JS_str(s))
#define SET_OBJECT(object,s,v) object->Set(JS_str(s), v)
#define GET_OBJECT(object,s) object->Get(JS_str(s))->ToObject()

#define GET_INT(object, name) GET_VALUE(object, name)->IntegerValue()

void SET_VALUE_AT_INDEX(v8::Handle<v8::Object> object, int index, v8::Handle<v8::Value> value) {
char buf[1024];
snprintf(buf, sizeof(buf), "%d", index);
SET_OBJECT(object, buf, value);
}

#define CLASS(NAME) NAME ## _class()

#define CONSTRUCTOR FUNCTION

#define PROTECT_OBJECT(value) Persistent<Object>::New(value)

#define HANDLE_EXCEPTION(a, b)

#define TO_OBJECT(object) (object->ToObject())
#define GET_BOOL(object, name ) (GET_VALUE(object, name)->BooleanValue())

#endif
11 changes: 0 additions & 11 deletions lib/binary-engine.js

This file was deleted.

20 changes: 20 additions & 0 deletions lib/file-engine.js
@@ -0,0 +1,20 @@
var file = require("file");

exports.mkdirs = function(path) {
var components = file.Path(path).split();
for (var i = 0; i < components.length; i++) {
var dir = file.join.apply(null, components.slice(0, i+1));
if (!file.isDirectory(dir))
file.mkdir(dir);
}
}

exports.touch = function (path, mtime) {
if (mtime === undefined || mtime === null)
mtime = new Date();

if (!file.exists(path))
file.write(path, "");

file.touchImpl(path, mtime.getTime());
};

0 comments on commit b32de1e

Please sign in to comment.