Subversion Repositories HomeAutomation

Rev

Rev 1619 | Rev 1642 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1619 Rev 1625
Line 108... Line 108...
108
    if (this->functions_.find(name) == this->functions_.end())
108
    if (this->functions_.find(name) == this->functions_.end())
109
    {
109
    {
110
        this->SendResponse(plugin_name, request_id, "No such function found, " + name);
110
        this->SendResponse(plugin_name, request_id, "No such function found, " + name);
111
        return;
111
        return;
112
    }
112
    }
-
 
113
 
-
 
114
    LOG.Debug("Call: " + name);
113
 
115
 
114
    Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data());
116
    Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data());
115
   
117
   
116
    if (result.IsEmpty())
118
    if (result.IsEmpty())
117
    {
119
    {
Line 148... Line 150...
148
    }
150
    }
149
   
151
   
150
    this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template);
152
    this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template);
151
   
153
   
152
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
154
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
153
    {
155
    {
154
        this->plugins_[c]->InitializeDone();
156
        this->plugins_[c]->InitializeDone();
155
    }
157
    }
156
   
158
   
157
    LOG.Info("VM initialized.");
159
    LOG.Info("VM initialized.");
158
}
160
}
159
 
161
 
160
bool Manager::ImportFunction(std::string functionname)
162
bool Manager::ImportFunction(std::string functionname)
Line 167... Line 169...
167
    v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name);
169
    v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name);
168
   
170
   
169
    if (!process_val->IsFunction())
171
    if (!process_val->IsFunction())
170
    {
172
    {
171
        LOG.Error(functionname + " was not found to be a function!");
173
        LOG.Error(functionname + " was not found to be a function!");
172
        return false;
174
        return false;
173
    }
175
    }
174
   
176
   
175
    v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val);
177
    v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val);
176
   
178
   
177
    this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun);
179
    this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun);
178
   
180
   
179
    return true;
181
    return true;
180
}
182
}
181
 
183
 
182
bool Manager::LoadScript(std::string scriptname)
184
bool Manager::LoadScript(std::string scriptname)
183
{
185
{
184
    std::string path = this->script_path_ + scriptname;
186
    std::string path = this->script_path_ + scriptname;
185
    std::ifstream file(path.data());
187
    std::ifstream file(path.data());
186
   
188
   
187
    if (!file.is_open())
189
    if (!file.is_open())
188
    {
190
    {
189
        file.close();
191
        file.close();
190
        LOG.Warning("Could not load " + scriptname + ".");
192
        LOG.Warning("Could not load " + scriptname + ".");
191
        return false;
193
        return false;
192
    }
194
    }
193
   
195
   
194
    std::string code = "";
196
    std::string code = "";
195
    std::string line;
197
    std::string line;
196
   
198
   
197
    while (!file.eof())
199
    while (!file.eof())
198
    {
200
    {
199
        std::getline(file, line);
201
        std::getline(file, line);
200
       
202
       
201
        if (file.eof())
203
        if (file.eof())
Line 203... Line 205...
203
            break;
205
            break;
204
        }
206
        }
205
       
207
       
206
        code += line + "\n";
208
        code += line + "\n";
207
    }
209
    }
208
   
210
   
209
    code += "\n";
211
    code += "\n";
210
   
212
   
211
    file.close();
213
    file.close();
212
   
214
   
213
    v8::Context::Scope context_scope(this->context_);
215
    v8::Context::Scope context_scope(this->context_);
214
   
216
   
215
    v8::TryCatch try_catch;
217
    v8::TryCatch try_catch;
216
   
218
   
217
    v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()),
219
    v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()),
218
                                                        v8::String::New(scriptname.data(), scriptname.length()));
220
                                                        v8::String::New(scriptname.data(), scriptname.length()));
219
   
221
   
Line 228... Line 230...
228
       
230
       
229
        if (result.IsEmpty())
231
        if (result.IsEmpty())
230
        {
232
        {
231
            LOG.Error(this->FormatException(try_catch));
233
            LOG.Error(this->FormatException(try_catch));
232
            return false;
234
            return false;
233
        }
235
        }
234
    }
236
    }
235
   
237
   
236
    LOG.Info("Loaded " + scriptname + " successfully.");
238
    LOG.Info("Loaded " + scriptname + " successfully.");
237
    return true;
239
    return true;
238
}
240
}
Line 269... Line 271...
269
    }
271
    }
270
}
272
}
271
 
273
 
272
std::string Manager::FormatException(v8::TryCatch& try_catch)
274
std::string Manager::FormatException(v8::TryCatch& try_catch)
273
{
275
{
-
 
276
    v8::Context::Scope context_scope(this->context_);
-
 
277
   
274
    std::string result;
278
    std::string result;
275
   
279
   
276
    v8::HandleScope handle_scope;
280
    v8::HandleScope handle_scope;
277
    v8::String::Utf8Value exception(try_catch.Exception());
281
    v8::String::Utf8Value exception(try_catch.Exception());
278
    v8::Handle<v8::Message> message = try_catch.Message();
282
    v8::Handle<v8::Message> message = try_catch.Message();