root/lang/cplusplus/fcgi-v8/trunk/src/core.cc @ 18872

Revision 18872, 4.7 kB (checked in by tokuhirom, 6 years ago)

change directory structures

Line 
1#include <v8.h>
2#include <cstdio>
3#include <cstdlib>
4#include <memory.h>
5#include <curl/curl.h>
6#include <iconv.h>
7#include <errno.h>
8#include "v8-util.h"
9
10extern int __argc;
11extern char ** __argv;
12
13extern v8::Handle<v8::Value> UrlFetch(const v8::Arguments& args);
14
15int utf_char2bytes(int c, unsigned char* buf) {
16    if (c < 0x80) {
17        buf[0] = c;
18        return 1;
19    }
20    if (c < 0x800) {
21        buf[0] = 0xc0 + ((unsigned)c >> 6);
22        buf[1] = 0x80 + (c & 0x3f);
23        return 2;
24    }
25    if (c < 0x10000) {
26        buf[0] = 0xe0 + ((unsigned)c >> 12);
27        buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
28        buf[2] = 0x80 + (c & 0x3f);
29        return 3;
30    }
31    if (c < 0x200000) {
32        buf[0] = 0xf0 + ((unsigned)c >> 18);
33        buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
34        buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
35        buf[3] = 0x80 + (c & 0x3f);
36        return 4;
37    }
38    if (c < 0x4000000) {
39        buf[0] = 0xf8 + ((unsigned)c >> 24);
40        buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
41        buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
42        buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
43        buf[4] = 0x80 + (c & 0x3f);
44        return 5;
45    }
46    /* 31 bits */
47    buf[0] = 0xfc + ((unsigned)c >> 30);
48    buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
49    buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
50    buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
51    buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
52    buf[5] = 0x80 + (c & 0x3f);
53    return 6;
54}
55
56char* ucs2utf_alloc(uint16_t *instr) {
57    uint16_t *p = instr;
58    int inlen = 0;
59
60    while(*p++) inlen++;
61    char* outstr = new char[inlen * MB_LEN_MAX];
62    char* outp = outstr;
63    memset(outstr, 0, inlen * MB_LEN_MAX);
64
65    p = instr;
66    while (*p) outstr += utf_char2bytes(*p++, (unsigned char*) outstr);
67    return outp;
68}
69
70v8::Handle<v8::Value> P(const v8::Arguments& args) {
71    for (int i = 0; i < args.Length(); i++) {
72        v8::HandleScope handle_scope;
73        v8::String::Value val(args[i]);
74        char* buf = ucs2utf_alloc(*val);
75        printf("%s", buf);
76        delete[] buf;
77    }
78    printf("\n");
79    return v8::Undefined();
80}
81
82v8::Handle<v8::Value> Print(const v8::Arguments& args) {
83    for (int i = 0; i < args.Length(); i++) {
84        v8::HandleScope handle_scope;
85        v8::String::Value val(args[i]);
86        char* buf = ucs2utf_alloc(*val);
87        printf("%s", buf);
88        delete[] buf;
89    }
90    printf("\n");
91    return v8::Undefined();
92}
93
94v8::Handle<v8::Value> Say(const v8::Arguments& args) {
95    for (int i = 0; i < args.Length(); i++) {
96        v8::HandleScope handle_scope;
97        v8::String::Value val(args[i]);
98        char* buf = ucs2utf_alloc(*val);
99        printf("%s", buf);
100        delete[] buf;
101    }
102    return v8::Undefined();
103}
104
105v8::Handle<v8::Value> Require(const v8::Arguments& args) {
106    for (int i = 0; i < args.Length(); i++) {
107        v8::HandleScope handle_scope;
108        v8::String::AsciiValue file(args[i]);
109        v8::Handle<v8::String> source = ReadFile(*file);
110        if (source.IsEmpty()) {
111            return v8::ThrowException(v8::String::New("Error loading file"));
112        }
113        ExecuteString(source, v8::String::New(*file), false);
114    }
115    return v8::Undefined();
116}
117
118v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
119    int exit_code = args[0]->Int32Value();
120    exit(exit_code);
121    return v8::Undefined();
122}
123
124v8::Handle<v8::Value> Version(const v8::Arguments& args) {
125    return v8::String::New(v8::V8::GetVersion());
126}
127
128v8::Handle<v8::Value> GetEnv(const v8::Arguments& args) {
129    v8::HandleScope handle_scope;
130    v8::String::AsciiValue str(args[0]);
131    const char * env = getenv(*str);
132    return env ? v8::String::New(env) : v8::Undefined();
133}
134
135// TODO: cache? or store to global variables?
136v8::Handle<v8::Value> ARGV(const v8::Arguments& args) {
137    v8::HandleScope handle_scope;
138    v8::Local<v8::Array> array = v8::Array::New();
139    for (int i=0; i<__argc; i++) {
140        array->Set(v8::Integer::New(i), v8::String::New(__argv[i]));
141    }
142    return array;
143}
144
145void setup_core(v8::Handle<v8::ObjectTemplate> target) {
146    target->Set(v8::String::New("p"), v8::FunctionTemplate::New(P));
147    target->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
148    target->Set(v8::String::New("say"), v8::FunctionTemplate::New(Say));
149    target->Set(v8::String::New("getenv"), v8::FunctionTemplate::New(GetEnv));
150    target->Set(v8::String::New("require"), v8::FunctionTemplate::New(Require));
151    target->Set(v8::String::New("exit"), v8::FunctionTemplate::New(Exit));
152    target->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version));
153    target->Set(v8::String::New("getargv"), v8::FunctionTemplate::New(ARGV));
154    target->Set(v8::String::New("urlfetch"), v8::FunctionTemplate::New(UrlFetch));
155}
156
Note: See TracBrowser for help on using the browser.