// standard node module
var execFile = require('child_process').execFile
// this launches the executable and returns immediately
var child = execFile("path to executable", ["arg1", "arg2"],
function (error, stdout, stderr) {
// This callback is invoked once the child terminates
// You'd want to check err/stderr as well!
console.log("Here is the complete output of the program: ");
console.log(stdout)
});
// if the program needs input on stdin, you can write to it immediately
child.stdin.setEncoding('utf-8');
child.stdin.write("Hello my child!\n");
var execFile = require('child_process').execFile
// this launches the executable and returns immediately
var child = execFile("path to executable", ["arg1", "arg2"],
function (error, stdout, stderr) {
// This callback is invoked once the child terminates
// You'd want to check err/stderr as well!
console.log("Here is the complete output of the program: ");
console.log(stdout)
});
// if the program needs input on stdin, you can write to it immediately
child.stdin.setEncoding('utf-8');
child.stdin.write("Hello my child!\n");
2.调用C++的dll
可以阅读node-ffi
调用的dll需要导出函数。
var ffi = require('ffi');
var libm = ffi.Library('libm', {
'ceil': [ 'double', [ 'double' ] ]
});
libm.ceil(1.5); // 2
// You can also access just functions in the current process by passing a null
var current = ffi.Library(null, {
'atoi': [ 'int', [ 'string' ] ]
});
current.atoi('1234'); // 1234
void CalculateResults(const v8::FunctionCallbackInfo&args) {
Isolate* isolate = args.GetIsolate();
std::vector locations;
std::vector results;
// extract each location (its a list)
Local input = Local::Cast(args[0]);
unsigned int num_locations = input->Length();
for (unsigned int i = 0; i < num_locations; i++) {
locations.push_back(unpack_location(isolate, Local::Cast(input->Get(i))));
}
// Build vector of rain_results
results.resize(locations.size());
std::transform(locations.begin(), locations.end(), results.begin(), calc_rain_stats);
// Convert the rain_results into Objects for return
Local result_list = Array::New(isolate);
for (unsigned int i = 0; i < results.size(); i++ ) {
Local result = Object::New(isolate);
pack_rain_result(isolate, result, results[i]);
result_list->Set(i, result);
}
// Return the list
args.GetReturnValue().Set(result_list);
}
从代码中我们看出来下面两行代码分别表示拿数据和返回数组
Local input = Local::Cast(args[0]);
Local result_list = Array::New(isolate);
void CalculateResultsAsync(const v8::FunctionCallbackInfo&args) {
Isolate* isolate = args.GetIsolate();
Work * work = new Work();
work->request.data = work;
// extract each location (its a list) and store it in the work package
// locations is on the heap, accessible in the libuv threads
Local input = Local::Cast(args[0]);
unsigned int num_locations = input->Length();
for (unsigned int i = 0; i < num_locations; i++) {
work->locations.push_back(unpack_location(isolate, Local::Cast(input->Get(i))));
}
// store the callback from JS in the work package so we can
// invoke it later
Local callback = Local::Cast(args[1]);
work->callback.Reset(isolate, callback);
// kick of the worker thread
uv_queue_work(uv_default_loop(),&work->request,WorkAsync,WorkAsyncComplete);
args.GetReturnValue().Set(Undefined(isolate));
}
我们看一下关键代码
Work * work = new Work();//堆上创建数据,可以在线程间共享
uv_queue_work(uv_default_loop(),&work->request,WorkAsync,WorkAsyncComplete);
struct Work {
uv_work_t request;
Persistent callback;
std::vector locations;
std::vector results;
};
// called by libuv worker in separate thread
static void WorkAsync(uv_work_t *req)
{
Work *work = static_cast(req->data);
// this is the worker thread, lets build up the results
// allocated results from the heap because we'll need
// to access in the event loop later to send back
work->results.resize(work->locations.size());
std::transform(work->locations.begin(), work->locations.end(), work->results.begin(), calc_rain_stats);
// that wasn't really that long of an operation, so lets pretend it took longer...
std::this_thread::sleep_for(chrono::seconds(3));
}
注意从uv_work_t拿到我们要操作的数据,线程之间可以共享堆上的数据,所以这里访问没有问题。
再看看回调如何执行。
// called by libuv in event loop when async function completes
static void WorkAsyncComplete(uv_work_t *req,int status)
{
Isolate * isolate = Isolate::GetCurrent();
// Fix for Node 4.x - thanks to https://github.com/nwjs/blink/commit/ecda32d117aca108c44f38c8eb2cb2d0810dfdeb
v8::HandleScope handleScope(isolate);
Local result_list = Array::New(isolate);
Work *work = static_cast(req->data);
// the work has been done, and now we pack the results
// vector into a Local array on the event-thread's stack.
for (unsigned int i = 0; i < work->results.size(); i++ ) {
Local result = Object::New(isolate);
pack_rain_result(isolate, result, work->results[i]);
result_list->Set(i, result);
}
// set up return arguments
Handle argv[] = { result_list };
// execute the callback
// https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065
Local::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
// Free up the persistent function callback
work->callback.Reset();
delete work;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml&q
// for循环的进化
// 菜鸟
for (var i = 0; i < Things.length ; i++) {
// Things[i]
}
// 老鸟
for (var i = 0, len = Things.length; i < len; i++) {
// Things[i]
}
// 大师
for (var i = Things.le
the idea is from:
http://blog.csdn.net/zhanxinhang/article/details/6731134
public class MaxSubMatrix {
/**see http://blog.csdn.net/zhanxinhang/article/details/6731134
* Q35
求一个矩阵中最大的二维
使用cordova可以很方便的在手机sdcard中读写文件。
首先需要安装cordova插件:file
命令为:
cordova plugin add org.apache.cordova.file
然后就可以读写文件了,这里我先是写入一个文件,具体的JS代码为:
var datas=null;//datas need write
var directory=&
SELECT cust_id,
SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250
db.orders.aggregate( [
{ $match: { status: 'A' } },
{
$group: {