summaryrefslogtreecommitdiff
path: root/src/bin/efl_js/launcher_main.cc
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2014-09-01 15:08:49 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2015-12-23 23:59:40 -0200
commita3db1dddd3ba67c81118f7f2c0bc753dc8aac551 (patch)
tree233ee1be7bfa299bff560207135d20940c4e411f /src/bin/efl_js/launcher_main.cc
parent1a3cb45f1cc7fdf8d481879e6bd7349d9cb0b3fa (diff)
efl-js: JavaScript Eolian binding
To configure efl sources with bindings to use in nodejs add ––with-js=nodejs in configure flags to generate node files $ configure --with-js=nodejs and compile normally with: $ make $ make install To use, you have to require efl: efl = require('efl') The bindings is divided in two parts: generated and manually written. The generation uses the Eolian library for parsing Eo files and generate C++ code that is compiled against V8 interpreter library to create a efl.node file that can be required in a node.js instance. @feature
Diffstat (limited to '')
-rw-r--r--src/bin/efl_js/launcher_main.cc156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/bin/efl_js/launcher_main.cc b/src/bin/efl_js/launcher_main.cc
new file mode 100644
index 0000000000..680f16ca52
--- /dev/null
+++ b/src/bin/efl_js/launcher_main.cc
@@ -0,0 +1,156 @@
1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <iostream>
6#include <fstream>
7#include <sstream>
8#include <string>
9#include <cerrno>
10
11#include <Eo_Js.hh>
12#include <Eina.hh>
13#include <Eo.hh>
14// #include <efl_js.hh>
15
16using namespace std;
17using namespace v8;
18
19const char PATH_SEPARATOR =
20#ifdef _WIN32
21 '\\';
22#else
23 '/';
24#endif
25
26static std::string get_file_contents(const char *filename) {
27 std::ifstream in(filename, std::ios::in);
28 if (in) {
29 std::ostringstream contents;
30 contents << in.rdbuf();
31 in.close();
32 return contents.str();
33 } else {
34 throw(errno);
35 }
36}
37
38static std::string get_filename(std::string path)
39{
40 int beginIdx = path.rfind(PATH_SEPARATOR);
41 return path.substr(beginIdx + 1);
42}
43
44static void show_usage(std::string name)
45{
46 std::cerr << "Usage: " << get_filename(name) << " <option(s)> [SOURCE]\n" << std::endl
47 << "Options:" << std::endl
48 << "\t-h, --help\t\t Show this help message" << std::endl;
49}
50
51/*
52 * Basic console.log implementation with space-separated values,
53 * no substitution
54 */
55void Log(const FunctionCallbackInfo<Value>& args)
56{
57 Isolate* isolate = Isolate::GetCurrent();
58 HandleScope scope(isolate);
59
60 for (int i=0; i < args.Length(); i++)
61 {
62 if (i != 0)
63 std::cout << " ";
64 String::Utf8Value string(args[i]);
65 std::cout << *string;
66 }
67
68 std::cout << std::endl;
69
70 args.GetReturnValue().Set(v8::Null(isolate));
71}
72
73
74int main(int argc, char* argv[])
75{
76
77 std::string script_source;
78 char *filename = 0;
79
80 for (int i=1; i < argc; i++)
81 {
82 if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0))
83 {
84 show_usage(argv[0]);
85 return 0;
86 }
87 else
88 {
89 filename = argv[i];
90 }
91 }
92
93 if (!filename)
94 {
95 std::cerr << "Error: No source provided." << std::endl;
96 show_usage(argv[0]);
97 return 1;
98 }
99
100 try
101 {
102 script_source = get_file_contents(filename);
103 } catch (int errno)
104 {
105 perror("Error: ");
106 return 1;
107 }
108
109
110 efl::eina::js::compatibility_initialize();
111 v8::V8::SetFlagsFromCommandLine(&argc, const_cast<char**>(argv), true);
112
113 v8::Isolate* isolate = efl::eina::js::compatibility_isolate_new();
114 {
115 Isolate::Scope isolate_scope(isolate);
116 HandleScope handleScope(isolate);
117
118 Local<Context> context = Context::New(isolate, NULL);
119 Context::Scope context_scope(context);
120 context->Enter();
121
122 // Setup the console and log
123 Local<Object> console = Object::New(isolate);
124 Local<FunctionTemplate> log = FunctionTemplate::New(isolate, Log);
125 console->Set(String::NewFromUtf8(isolate, "log"), log->GetFunction());
126
127 Local<Object> global = context->Global();
128 global->Set(String::NewFromUtf8(isolate, "console"), console);
129
130 // Set up the efl exports; Needed to enter the context before this
131 // due to creating Objects instead of Objects Templates
132 // WIP: Commented out due to potential missing v8 platform implementation issues
133 // Local<Object> efl_exports = Object::New(isolate);
134 // global->Set(String::NewFromUtf8(isolate, "efl"), efl_exports);
135 // efl_js::init(efl_exports);
136
137 // And now the user's script
138 Local<String> source = String::NewFromUtf8(isolate, script_source.c_str());
139
140 Local<Script> script = Script::Compile(source);
141
142 TryCatch tryCatch(isolate);
143 Local<Value> result = script->Run();
144
145 if (result.IsEmpty())
146 {
147 Local<Value> exception = tryCatch.Exception();
148 String::Utf8Value exception_str(exception);
149 printf("Exception: %s\n", *exception_str);
150 }
151
152 }
153
154 V8::Dispose();
155 return 0;
156}