-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TIMOB-9874: BlackBerry: implement require() #96
Changes from 1 commit
55239f1
1411848
a51e743
9c70679
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
#include "TiV8EventContainerFactory.h" | ||
#include <fstream> | ||
|
||
#include <fstream> | ||
#include <sstream> | ||
|
||
static Handle<ObjectTemplate> g_rootTemplate; | ||
|
||
TiRootObject::TiRootObject() | ||
|
@@ -98,9 +101,10 @@ int TiRootObject::executeScript(NativeObjectFactory* objectFactory, const char* | |
Context::Scope context_scope(context_); | ||
initializeTiObject(NULL); | ||
|
||
const char* bootstrapFilename = "bootstrap.js"; | ||
string bootstrapJavascript; | ||
{ | ||
ifstream ifs("app/native/framework/bootstrap.js"); | ||
ifstream ifs((string("app/native/framework/") + bootstrapFilename).c_str()); | ||
if (!ifs) | ||
{ | ||
TiLogger::getInstance().log(Ti::Msg::ERROR__Cannot_load_bootstrap_js); | ||
|
@@ -111,7 +115,7 @@ int TiRootObject::executeScript(NativeObjectFactory* objectFactory, const char* | |
} | ||
|
||
TryCatch tryCatch; | ||
Handle<Script> compiledBootstrapScript = Script::Compile(String::New(bootstrapJavascript.c_str())); | ||
Handle<Script> compiledBootstrapScript = Script::Compile(String::New(bootstrapJavascript.c_str()), String::New(bootstrapFilename)); | ||
if (compiledBootstrapScript.IsEmpty()) | ||
{ | ||
String::Utf8Value error(tryCatch.Exception()); | ||
|
@@ -121,12 +125,23 @@ int TiRootObject::executeScript(NativeObjectFactory* objectFactory, const char* | |
Handle<Value> bootstrapResult = compiledBootstrapScript->Run(); | ||
if (bootstrapResult.IsEmpty()) | ||
{ | ||
String::Utf8Value error(tryCatch.Exception()); | ||
TiLogger::getInstance().log(*error); | ||
Local<Value> exception = tryCatch.Exception(); | ||
if (exception->IsString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that something, that could happen? I don't think it could be string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put a FIXME to handle this better |
||
{ | ||
TiLogger::getInstance().log(*String::Utf8Value(exception)); | ||
} | ||
else | ||
{ | ||
Handle<Message> msg = tryCatch.Message(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should handle the case when message is empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
stringstream ss; | ||
ss << bootstrapFilename << " line " << msg->GetLineNumber() << ": " << *String::Utf8Value(exception); | ||
TiLogger::getInstance().log(ss.str()); | ||
} | ||
return -1; | ||
} | ||
|
||
Handle<Script> compiledScript = Script::Compile(String::New(javaScript)); | ||
const char* filename = "app.js"; | ||
Handle<Script> compiledScript = Script::Compile(String::New(javaScript), String::New(filename)); | ||
if (compiledScript.IsEmpty()) | ||
{ | ||
String::Utf8Value error(tryCatch.Exception()); | ||
|
@@ -136,8 +151,18 @@ int TiRootObject::executeScript(NativeObjectFactory* objectFactory, const char* | |
Handle<Value> result = compiledScript->Run(); | ||
if (result.IsEmpty()) | ||
{ | ||
String::Utf8Value error(tryCatch.Exception()); | ||
TiLogger::getInstance().log(*error); | ||
Local<Value> exception = tryCatch.Exception(); | ||
if (exception->IsString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above. |
||
{ | ||
TiLogger::getInstance().log(*String::Utf8Value(exception)); | ||
} | ||
else | ||
{ | ||
Handle<Message> msg = tryCatch.Message(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
stringstream ss; | ||
ss << filename << " line " << msg->GetLineNumber() << ": " << *String::Utf8Value(exception); | ||
TiLogger::getInstance().log(ss.str()); | ||
} | ||
return -1; | ||
} | ||
onStartMessagePump(); | ||
|
@@ -192,9 +217,67 @@ Handle<Value> TiRootObject::_clearTimeout(void*, TiObject*, const Arguments& arg | |
|
||
Handle<Value> TiRootObject::_require(void*, TiObject*, const Arguments& args) | ||
{ | ||
// TODO: finish this | ||
(void)args; | ||
return Undefined(); | ||
if (args.Length() < 1) | ||
{ | ||
return ThrowException(String::New(Ti::Msg::Missing_argument)); | ||
} | ||
|
||
Handle<String> v8Filename = args[0]->ToString(); | ||
string id = *String::Utf8Value(v8Filename); | ||
|
||
// check if cached | ||
static map<string, Persistent<Value> > cache; | ||
map<string, Persistent<Value> >::const_iterator cachedValue = cache.find(id); | ||
if (cachedValue != cache.end()) | ||
{ | ||
return cachedValue->second; | ||
} | ||
|
||
string filename = id + ".js"; | ||
// TODO: need to make this relative | ||
static const string baseFolder = "app/native/assets/"; | ||
string javascript; | ||
{ | ||
ifstream ifs((baseFolder + filename).c_str()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not handle the cases when module should load relative to current. So, for example having ui/module1 ui/module2. In app.js having require('ui/module1') in module1 having require('module2'). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a TODO for that above |
||
if (!ifs) | ||
{ | ||
Local<Value> taggedMessage = String::Concat(String::New("No such native module "), v8Filename); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this error message to message strings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return ThrowException(taggedMessage); | ||
} | ||
getline(ifs, javascript, string::traits_type::to_char_type(string::traits_type::eof())); | ||
ifs.close(); | ||
} | ||
|
||
// wrap the module | ||
{ | ||
static const string preWrap = "(function () { var exports = {};\n"; | ||
static const string postWrap = "\nreturn exports; })();"; | ||
javascript = preWrap + javascript + postWrap; | ||
} | ||
|
||
// TODO: set the correct context | ||
|
||
TryCatch tryCatch; | ||
Handle<Script> compiledScript = Script::Compile(String::New(javascript.c_str()), v8Filename); | ||
if (compiledScript.IsEmpty()) | ||
{ | ||
return ThrowException(tryCatch.Exception()); | ||
} | ||
Persistent<Value> result = (Persistent<Value>)compiledScript->Run(); | ||
if (result.IsEmpty()) | ||
{ | ||
Handle<Message> msg = tryCatch.Message(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should handle the case when message is empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
int lineNumber = msg->GetLineNumber() - 1; // -1 for the wrapper | ||
stringstream ss; | ||
ss << filename << " line " << lineNumber << ": "; | ||
Local<String> exceptionString = String::Concat(String::New(ss.str().c_str()), tryCatch.Exception()->ToString()); | ||
return ThrowException(exceptionString); | ||
} | ||
|
||
// cache result | ||
cache.insert(pair<string, Persistent<Value> >(id, result)); | ||
|
||
return result; | ||
} | ||
|
||
Handle<Value> TiRootObject::_setInterval(void*, TiObject*, const Arguments& args) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ bool TiTitaniumObject::canAddMembers() const | |
return false; | ||
} | ||
|
||
Handle<Value> TiTitaniumObject::_include(void* userContext, TiObject* caller, const Arguments& args) | ||
Handle<Value> TiTitaniumObject::_include(void*, TiObject*, const Arguments& args) | ||
{ | ||
if (args.Length() < 1) | ||
{ | ||
|
@@ -94,10 +94,14 @@ Handle<Value> TiTitaniumObject::_include(void* userContext, TiObject* caller, co | |
{ | ||
return ThrowException(tryCatch.Exception()); | ||
} | ||
Handle<Value>result = compiledScript->Run(); | ||
Handle<Value> result = compiledScript->Run(); | ||
if (result.IsEmpty()) | ||
{ | ||
return ThrowException(tryCatch.Exception()); | ||
Handle<Message> msg = tryCatch.Message(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should handle the case when message is empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
stringstream ss; | ||
ss << filename << " line " << msg->GetLineNumber() << ": "; | ||
Local<String> exceptionString = String::Concat(String::New(ss.str().c_str()), tryCatch.Exception()->ToString()); | ||
return ThrowException(exceptionString); | ||
} | ||
|
||
// Reset relative path | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate include
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed