summaryrefslogtreecommitdiff
path: root/src/bin/efl_js/launcher_main.cc
blob: 680f16ca52578fa18a35a2f10ebc11d7984bba5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cerrno>

#include <Eo_Js.hh>
#include <Eina.hh>
#include <Eo.hh>
// #include <efl_js.hh>

using namespace std;
using namespace v8;

const char PATH_SEPARATOR =
#ifdef _WIN32
    '\\';
#else
    '/';
#endif

static std::string get_file_contents(const char *filename) {
    std::ifstream in(filename, std::ios::in);
    if (in) {
        std::ostringstream contents;
        contents << in.rdbuf();
        in.close();
        return contents.str();
    } else {
        throw(errno);
    }
}

static std::string get_filename(std::string path)
{
    int beginIdx = path.rfind(PATH_SEPARATOR);
    return path.substr(beginIdx + 1);
}

static void show_usage(std::string name)
{
    std::cerr << "Usage: " << get_filename(name) << " <option(s)> [SOURCE]\n" << std::endl
              << "Options:" << std::endl
              << "\t-h, --help\t\t Show this help message" << std::endl;
}

/*
 * Basic console.log implementation with space-separated values,
 * no substitution
 */
void Log(const FunctionCallbackInfo<Value>& args)
{
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    for (int i=0; i < args.Length(); i++)
    {
        if (i != 0)
            std::cout << " ";
        String::Utf8Value string(args[i]);
        std::cout << *string;
    }

    std::cout << std::endl;

    args.GetReturnValue().Set(v8::Null(isolate));
}


int main(int argc, char* argv[])
{

    std::string script_source;
    char *filename = 0;

    for (int i=1; i < argc; i++)
    {
        if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0))
        {
            show_usage(argv[0]);
            return 0;
        }
        else
        {
            filename = argv[i];
        }
    }

    if (!filename)
    {
        std::cerr << "Error: No source provided." << std::endl;
        show_usage(argv[0]);
        return 1;
    }

    try
    {
        script_source = get_file_contents(filename);
    } catch (int errno)
    {
        perror("Error: ");
        return 1;
    }


    efl::eina::js::compatibility_initialize();
    v8::V8::SetFlagsFromCommandLine(&argc, const_cast<char**>(argv), true);

    v8::Isolate* isolate = efl::eina::js::compatibility_isolate_new();
    {
        Isolate::Scope isolate_scope(isolate);
        HandleScope handleScope(isolate);

        Local<Context> context = Context::New(isolate, NULL);
        Context::Scope context_scope(context);
        context->Enter();

        // Setup the console and log
        Local<Object> console = Object::New(isolate);
        Local<FunctionTemplate> log = FunctionTemplate::New(isolate, Log);
        console->Set(String::NewFromUtf8(isolate, "log"), log->GetFunction());

        Local<Object> global = context->Global();
        global->Set(String::NewFromUtf8(isolate, "console"), console);

        // Set up the efl exports; Needed to enter the context before this
        // due to creating Objects instead of Objects Templates
        // WIP: Commented out due to potential missing v8 platform implementation issues
        // Local<Object> efl_exports = Object::New(isolate);
        // global->Set(String::NewFromUtf8(isolate, "efl"), efl_exports);
        // efl_js::init(efl_exports);

        // And now the user's script
        Local<String> source = String::NewFromUtf8(isolate, script_source.c_str());

        Local<Script> script = Script::Compile(source);

        TryCatch tryCatch(isolate);
        Local<Value> result = script->Run();

        if (result.IsEmpty())
        {
            Local<Value> exception = tryCatch.Exception();
            String::Utf8Value exception_str(exception);
            printf("Exception: %s\n", *exception_str);
        }

    }

    V8::Dispose();
    return 0;
}