Made it possible to require() a script from another package. It's high time for some code reuse.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3191 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
parent
df2c0b108f
commit
560df7e5f3
10
core/core.js
10
core/core.js
@ -215,6 +215,11 @@ function getManifest(fileName) {
|
|||||||
return manifest.length ? JSON.parse(manifest.join("\n")) : null;
|
return manifest.length ? JSON.parse(manifest.join("\n")) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function packageNameToPath(name) {
|
||||||
|
var process = this;
|
||||||
|
return "packages/" + process.packageOwner + "/" + name + "/";
|
||||||
|
}
|
||||||
|
|
||||||
function getProcess(packageOwner, packageName, key, options) {
|
function getProcess(packageOwner, packageName, key, options) {
|
||||||
var process = gProcesses[key];
|
var process = gProcesses[key];
|
||||||
if (!process
|
if (!process
|
||||||
@ -342,6 +347,11 @@ function getProcess(packageOwner, packageName, key, options) {
|
|||||||
throw new Error(packageOwner + " does not have right to permission 'network'.");
|
throw new Error(packageOwner + " does not have right to permission 'network'.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (manifest && manifest.require) {
|
||||||
|
print("manifest.require = ", manifest.require);
|
||||||
|
print(manifest.require.map(packageNameToPath.bind(process)));
|
||||||
|
process.task.addPath(manifest.require.map(packageNameToPath.bind(process)));
|
||||||
|
}
|
||||||
process.task.setImports(imports);
|
process.task.setImports(imports);
|
||||||
print("Activating task");
|
print("Activating task");
|
||||||
process.task.activate();
|
process.task.activate();
|
||||||
|
39
src/Task.cpp
39
src/Task.cpp
@ -182,6 +182,11 @@ void Task::activate() {
|
|||||||
|
|
||||||
v8::Local<v8::Context> context = v8::Context::New(_isolate, 0, global);
|
v8::Local<v8::Context> context = v8::Context::New(_isolate, 0, global);
|
||||||
_context = v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context> >(_isolate, context);
|
_context = v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context> >(_isolate, context);
|
||||||
|
|
||||||
|
v8::Context::Scope contextScope(v8::Local<v8::Context>::New(_isolate, _context));
|
||||||
|
|
||||||
|
v8::Local<v8::Object> exportObject = v8::Object::New(_isolate);
|
||||||
|
_exportObject = v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object> >(_isolate, exportObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Task::activate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
void Task::activate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||||
@ -270,6 +275,16 @@ bool Task::execute(const char* fileName) {
|
|||||||
if (!_scriptName.size()) {
|
if (!_scriptName.size()) {
|
||||||
_scriptName = fileName;
|
_scriptName = fileName;
|
||||||
}
|
}
|
||||||
|
if (!_path.size()) {
|
||||||
|
std::string path = _scriptName;
|
||||||
|
size_t position = path.rfind('/');
|
||||||
|
if (position != std::string::npos) {
|
||||||
|
path.resize(position + 1);
|
||||||
|
} else {
|
||||||
|
path = ".";
|
||||||
|
}
|
||||||
|
_path.push_back(path);
|
||||||
|
}
|
||||||
if (!source.IsEmpty()) {
|
if (!source.IsEmpty()) {
|
||||||
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
|
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
|
||||||
if (!script.IsEmpty()) {
|
if (!script.IsEmpty()) {
|
||||||
@ -645,6 +660,23 @@ void Task::onReceivePacket(int packetType, const char* begin, size_t length, voi
|
|||||||
to->_trusted = trusted;
|
to->_trusted = trusted;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case kAddPath:
|
||||||
|
{
|
||||||
|
v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(Serialize::load(to, from, std::vector<char>(begin, begin + length)));
|
||||||
|
if (!result.IsEmpty()) {
|
||||||
|
for (size_t i = 0; i < result->Length(); ++i) {
|
||||||
|
v8::Handle<v8::Value> handle = result->Get(i);
|
||||||
|
if (!handle.IsEmpty()) {
|
||||||
|
v8::Handle<v8::String> entry = handle.As<v8::String>();
|
||||||
|
if (!entry.IsEmpty()) {
|
||||||
|
v8::String::Utf8Value value(entry);
|
||||||
|
to->_path.push_back(*value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
case kActivate:
|
case kActivate:
|
||||||
to->activate();
|
to->activate();
|
||||||
break;
|
break;
|
||||||
@ -692,10 +724,9 @@ void Task::configureFromStdin() {
|
|||||||
|
|
||||||
std::string Task::resolveRequire(const std::string& require) {
|
std::string Task::resolveRequire(const std::string& require) {
|
||||||
std::string result;
|
std::string result;
|
||||||
std::string path = _scriptName;
|
|
||||||
size_t position = path.rfind('/');
|
for (size_t i = 0; i < _path.size(); ++i) {
|
||||||
if (position != std::string::npos) {
|
std::string& path = _path[i];
|
||||||
path.resize(position + 1);
|
|
||||||
std::cout << "Looking in " << path << " for " << require << "\n";
|
std::cout << "Looking in " << path << " for " << require << "\n";
|
||||||
if (require.find("..") == std::string::npos && require.find('/') == std::string::npos) {
|
if (require.find("..") == std::string::npos && require.find('/') == std::string::npos) {
|
||||||
result = path + require;
|
result = path + require;
|
||||||
|
@ -31,6 +31,7 @@ enum MessageType {
|
|||||||
kReleaseExport,
|
kReleaseExport,
|
||||||
kReleaseImport,
|
kReleaseImport,
|
||||||
kSetTrusted,
|
kSetTrusted,
|
||||||
|
kAddPath,
|
||||||
kActivate,
|
kActivate,
|
||||||
kExecute,
|
kExecute,
|
||||||
kKill,
|
kKill,
|
||||||
@ -95,6 +96,7 @@ private:
|
|||||||
taskid_t _nextTask = 1;
|
taskid_t _nextTask = 1;
|
||||||
static const taskid_t kParentId = 0;
|
static const taskid_t kParentId = 0;
|
||||||
std::map<taskid_t, TaskStub*> _children;
|
std::map<taskid_t, TaskStub*> _children;
|
||||||
|
std::vector<std::string> _path;
|
||||||
|
|
||||||
typedef std::map<std::string, v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object> > > ScriptExportMap;
|
typedef std::map<std::string, v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object> > > ScriptExportMap;
|
||||||
ScriptExportMap _scriptExports;
|
ScriptExportMap _scriptExports;
|
||||||
|
@ -91,6 +91,7 @@ void TaskStub::create(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||||||
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "execute"), v8::FunctionTemplate::New(args.GetIsolate(), TaskStub::execute, data));
|
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "execute"), v8::FunctionTemplate::New(args.GetIsolate(), TaskStub::execute, data));
|
||||||
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "kill"), v8::FunctionTemplate::New(args.GetIsolate(), TaskStub::kill, data));
|
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "kill"), v8::FunctionTemplate::New(args.GetIsolate(), TaskStub::kill, data));
|
||||||
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "statistics"), v8::FunctionTemplate::New(args.GetIsolate(), TaskStub::statistics, data));
|
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "statistics"), v8::FunctionTemplate::New(args.GetIsolate(), TaskStub::statistics, data));
|
||||||
|
taskTemplate->Set(v8::String::NewFromUtf8(args.GetIsolate(), "addPath"), v8::FunctionTemplate::New(args.GetIsolate(), addPath, data));
|
||||||
taskTemplate->SetInternalFieldCount(1);
|
taskTemplate->SetInternalFieldCount(1);
|
||||||
|
|
||||||
v8::Handle<v8::Object> taskObject = taskTemplate->NewInstance();
|
v8::Handle<v8::Object> taskObject = taskTemplate->NewInstance();
|
||||||
@ -195,6 +196,14 @@ void TaskStub::setImports(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TaskStub::addPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||||
|
if (TaskStub* stub = TaskStub::get(args.Data())) {
|
||||||
|
std::vector<char> buffer;
|
||||||
|
Serialize::store(Task::get(args.GetIsolate()), buffer, args[0]);
|
||||||
|
stub->_stream.send(kAddPath, &*buffer.begin(), buffer.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void TaskStub::getOnExit(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args) {
|
void TaskStub::getOnExit(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args) {
|
||||||
TaskTryCatch tryCatch(TaskStub::get(args.Data())->_owner);
|
TaskTryCatch tryCatch(TaskStub::get(args.Data())->_owner);
|
||||||
v8::HandleScope scope(args.GetIsolate());
|
v8::HandleScope scope(args.GetIsolate());
|
||||||
|
@ -47,6 +47,8 @@ private:
|
|||||||
static void getExports(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void getExports(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void setImports(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void setImports(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
|
|
||||||
|
static void addPath(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
|
|
||||||
static void getOnExit(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
|
static void getOnExit(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
|
||||||
static void setOnExit(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
|
static void setOnExit(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user