-
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-9539: BlackBerry: Implement include() #91
Changes from 2 commits
87fdb40
86e078a
414b97e
0ffaea7
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 |
---|---|---|
|
@@ -6,8 +6,13 @@ | |
*/ | ||
|
||
#include "TiTitaniumObject.h" | ||
#include "TiUIObject.h" | ||
#include "TiAPIObject.h" | ||
#include "TiGenericFunctionObject.h" | ||
#include "TiLogger.h" | ||
#include "TiMessageStrings.h" | ||
#include "TiUIObject.h" | ||
|
||
#include <fstream> | ||
|
||
TiTitaniumObject::TiTitaniumObject() | ||
: TiObject("Titanium") | ||
|
@@ -28,9 +33,10 @@ TiObject* TiTitaniumObject::createObject(NativeObjectFactory* objectFactory) | |
|
||
void TiTitaniumObject::onCreateStaticMembers() | ||
{ | ||
// TODO: remove hard coded version number | ||
ADD_STATIC_TI_VALUE("buildDate", String::New(__DATE__), this); | ||
ADD_STATIC_TI_VALUE("version", Number::New(2.0), this); | ||
// TODO: remove hard coded version number | ||
TiGenericFunctionObject::addGenericFunctionToParent(this, "include", this, _include); | ||
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. Put this line after the TODO, or better yet move the TODO onto or above the line it's talking about 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 |
||
TiUIObject::addObjectToParent(this, objectFactory_); | ||
TiAPIObject::addObjectToParent(this); | ||
} | ||
|
@@ -39,3 +45,63 @@ bool TiTitaniumObject::canAddMembers() const | |
{ | ||
return false; | ||
} | ||
|
||
Handle<Value> TiTitaniumObject::_include(void* userContext, TiObject* caller, const Arguments& args) | ||
{ | ||
if (args.Length() < 1) | ||
{ | ||
return ThrowException(String::New(Ti::Msg::Missing_argument)); | ||
} | ||
|
||
Local<Value> javaScript = args[0]; | ||
if (!javaScript->IsString()) | ||
{ | ||
javaScript = javaScript->ToString(); | ||
} | ||
|
||
static string sRelDir = ""; | ||
|
||
string base = "app/native/assets/"; | ||
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 can be static const 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 |
||
string fullPath = base + sRelDir + *String::Utf8Value(javaScript); | ||
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 there nothing in userContext, caller, or something in v8 that can tell us which file it is being called from? It could simplify things if we add that functionality. 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. TODO added. |
||
string filename = *String::Utf8Value(javaScript); | ||
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 can be done before and used in fullPath so there's less conversion 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. Why is sRelDir empty? Can it be changed? 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. @dlifshitz-maca : Done 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. @dcampbell-macadamian: It changed when included filename contains directory. See the "if (slash_pos != std::string::npos)" block. |
||
|
||
// Get the directory before slash | ||
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 an interesting way to approach relative directories, but I don't think it will work for windows with a url or event handlers. Windows with a url don't seem to be used much in KS and the docs say calling from an event handler "does not work consistently across platforms" so maybe we can get by with this for now. What I'd like to see is a TODO saying that this needs to be checked if it will work properly in those scenarios. 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. Actually, I'd also like you to take a look at android/runtime/common/src/js/titanium.js where it does 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. TODO added |
||
std::string::size_type slash_pos = filename.rfind("/"); | ||
if (slash_pos != std::string::npos) | ||
{ | ||
slash_pos++; | ||
sRelDir += filename.substr(0, slash_pos); | ||
} | ||
|
||
ifstream ifs(fullPath.c_str()); | ||
|
||
if (ifs.bad()) | ||
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 just realized this won't be true if the file does not exist. Use fail() instead, which also checks the badbit. 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 with !ifs. |
||
{ | ||
return ThrowException(String::New(Ti::Msg::Include_file_not_found)); | ||
} | ||
|
||
string buffer; | ||
getline(ifs, buffer, string::traits_type::to_char_type(string::traits_type::eof())); | ||
ifs.close(); | ||
|
||
TryCatch tryCatch; | ||
Handle<Script> compiledScript = Script::Compile(String::New(buffer.c_str())); | ||
if (compiledScript.IsEmpty()) | ||
{ | ||
String::Utf8Value error(tryCatch.Exception()); | ||
TiLogger::getInstance().log(string(*error) + "\n"); | ||
return ThrowException(tryCatch.Exception()); | ||
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 seems like duplicated effort. Won't ThrowException display the error in the log? 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. You should add the name of the file that can't be found to the exception. 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. Remove 1st logging. |
||
} | ||
Handle<Value>result = compiledScript->Run(); | ||
if (result.IsEmpty()) | ||
{ | ||
String::Utf8Value error(tryCatch.Exception()); | ||
TiLogger::getInstance().log(string(*error) + "\n"); | ||
return ThrowException(tryCatch.Exception()); | ||
} | ||
|
||
// Reset relative path | ||
sRelDir = ""; | ||
|
||
return Undefined(); | ||
} |
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.
Too far above. Should be exactly above the line with
New(2.0)
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.
Done :)