Skip to content
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

Merged
merged 4 commits into from
Jul 12, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion blackberry/tibb/NativeLoggerWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ using namespace std;

void NativeLoggerWorker::log(const QString& t)
{
fprintf(stderr, "%s\n", t.toStdString().c_str());
fprintf(stderr, "%s", (t + "\n").toStdString().c_str());
}
103 changes: 93 additions & 10 deletions blackberry/tibb/TiRootObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "TiV8EventContainerFactory.h"
#include <fstream>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate include

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


#include <fstream>
#include <sstream>

static Handle<ObjectTemplate> g_rootTemplate;

TiRootObject::TiRootObject()
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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())

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should handle the case when message is empty.

Copy link
Author

Choose a reason for hiding this comment

The 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());
Expand All @@ -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())

Choose a reason for hiding this comment

The 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();

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -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());

Choose a reason for hiding this comment

The 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').

Copy link
Author

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this error message to message strings

Copy link
Author

Choose a reason for hiding this comment

The 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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should handle the case when message is empty.

Copy link
Author

Choose a reason for hiding this comment

The 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)
Expand Down
10 changes: 7 additions & 3 deletions blackberry/tibb/TiTitaniumObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should handle the case when message is empty.

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down