Skip to content

Commit

Permalink
Merge pull request #91 from alexandergalstyan/includeBranch
Browse files Browse the repository at this point in the history
TIMOB-9539: BlackBerry: Implement include()
  • Loading branch information
alexandergalstyan committed Jul 9, 2012
2 parents 55a86c3 + 0ffaea7 commit 4ea1b23
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions blackberry/tibb/TiMessageStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Msg
{

TIMESSAGESTRINGS_CONST_DEF(char*, ERROR__Cannot_load_bootstrap_js, "ERROR: Cannot load bootstrap.js");
TIMESSAGESTRINGS_CONST_DEF(char*, Include_file_not_found, "Include file not found");
TIMESSAGESTRINGS_CONST_DEF(char*, INTERNAL__Global_String_symbol_is_not_an_object, "INTERNAL: Global String symbol is not an object");
TIMESSAGESTRINGS_CONST_DEF(char*, INTERNAL__args0_is_not_a_number, "args[0] is not a number.");
TIMESSAGESTRINGS_CONST_DEF(char*, Missing_argument, "Missing argument");
Expand Down
68 changes: 66 additions & 2 deletions blackberry/tibb/TiTitaniumObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -29,8 +34,9 @@ TiObject* TiTitaniumObject::createObject(NativeObjectFactory* objectFactory)
void TiTitaniumObject::onCreateStaticMembers()
{
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
ADD_STATIC_TI_VALUE("version", Number::New(2.0), this);
TiGenericFunctionObject::addGenericFunctionToParent(this, "include", this, _include);
TiUIObject::addObjectToParent(this, objectFactory_);
TiAPIObject::addObjectToParent(this);
}
Expand All @@ -39,3 +45,61 @@ 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 = "";

// TODO: Add functionality to grab all this information from userContext
static const string base = "app/native/assets/";
string filename = *String::Utf8Value(javaScript);
string fullPath = base + sRelDir + filename;

// TODO: Check this against url and event handlers
// Get the directory before slash
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)
{
return ThrowException(String::Concat(String::New(fullPath.c_str()), 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()), String::New(fullPath.c_str()));
if (compiledScript.IsEmpty())
{
return ThrowException(tryCatch.Exception());
}
Handle<Value>result = compiledScript->Run();
if (result.IsEmpty())
{
return ThrowException(tryCatch.Exception());
}

// Reset relative path
sRelDir = "";

return Undefined();
}
5 changes: 4 additions & 1 deletion blackberry/tibb/TiTitaniumObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class TiTitaniumObject : public TiObject
virtual bool canAddMembers() const;

private:
TiTitaniumObject();
explicit TiTitaniumObject();
TiTitaniumObject(const TiTitaniumObject&);
TiTitaniumObject& operator=(const TiTitaniumObject&);
static Handle<Value> _include(void* userContext, TiObject* caller, const Arguments& args);
NativeObjectFactory* objectFactory_;
};

Expand Down

0 comments on commit 4ea1b23

Please sign in to comment.