root/lang/cplusplus/fcgi-v8/branches/dlopen/src/platform-posix.cc @ 19315

Revision 19315, 1.9 kB (checked in by tokuhirom, 5 years ago)

added dlopen feature

Line 
1#include <v8.h>
2#include "v8-util.h"
3#include <sys/stat.h>
4#include <sys/types.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <dlfcn.h>
8
9int     __argc;
10char ** __argv;
11
12using namespace v8;
13
14static v8::Handle<v8::Value> Mkdir(const v8::Arguments& args) {
15    v8::HandleScope handle_scope;
16    if (! args[0]->IsString()) {
17        throw "mkdir needs string";
18    }
19    v8::String::AsciiValue str(args[0]);
20    mode_t mode = args[1]->Int32Value() || 0777;
21    return v8::Integer::New(mkdir(*str, mode));
22}
23
24static v8::Handle<v8::Value> Fork(const v8::Arguments& args) {
25    v8::HandleScope handle_scope;
26    pid_t pid = fork();
27    return v8::Integer::New(pid);
28}
29
30static v8::Handle<v8::Value> Sleep(const v8::Arguments& args) {
31    v8::HandleScope handle_scope;
32    sleep( args[1]->Int32Value() || 0 );
33    return v8::Undefined();
34}
35
36static v8::Handle<v8::Value> _load_dll(const v8::Arguments& args) {
37    assert_args(args, 1);
38    v8::HandleScope handle_scope;
39    void *handle;
40    v8::String::Utf8Value fname(args[0]);
41    if (!(handle = dlopen(*fname, RTLD_LAZY|RTLD_GLOBAL))) {
42        Handle<String> err = String::New(dlerror());
43        dlclose(handle);
44        return ThrowException(err);
45    }
46    void (*func)(v8::Handle<Object>);
47    if (!(func = reinterpret_cast<void(*)(Handle<Object>)>(dlsym(handle, "init_lib")))) {
48        Handle<String> err = String::New(dlerror());
49        dlclose(handle);
50        return ThrowException(err);
51    }
52    func(Context::GetCurrent()->Global());
53    dlclose(handle);
54    return Undefined();
55}
56
57void setup_syscall(v8::Handle<v8::ObjectTemplate> target) {
58    target->Set(v8::String::New("mkdir"),  v8::FunctionTemplate::New(Mkdir));
59    target->Set(v8::String::New("fork"),   v8::FunctionTemplate::New(Fork));
60    target->Set(v8::String::New("sleep"),  v8::FunctionTemplate::New(Sleep));
61    target->Set(v8::String::New("load_dll"),  v8::FunctionTemplate::New(_load_dll));
62}
63
Note: See TracBrowser for help on using the browser.