lua和V8 JavaScript初学中,,,,(二)

来源:互联网 发布:js自动获取input光标 编辑:程序博客网 时间:2024/05/21 15:47

接着上一篇啊。。。。

一,这个是用的nodejs写的,,,,

环境什么的看看这个http://www.cnblogs.com/CodeGuy/archive/2013/05/17/3083518.html这个写的还不错,,,

二,接下来看看代码吧

1,先看是vs2010的

//hw_v8.cpp
#define BUILDING_NODE_EXTENSION#include "node.h"#include "StdAfx.h"#include <fstream>using namespace v8;using namespace std;class file{//c++的类public:void createfile(){ofstream ocout;ocout.open("hello.txt");ocout<<"hello world";ocout.close();}};Handle<Value> Hello(const Arguments& args) {HandleScope scope;//创建本地作用域句柄Local<Object> self=args.Holder();Local<External> wrap=Local<External>::Cast(self->GetInternalField(0));void *ptr=wrap->Value();//调用类的方法static_cast<file *>(ptr)->createfile();return scope.Close(String::New("finished"));}void init(Handle<Object> target) {NODE_SET_METHOD(target, "hello", Hello);//在js文件中调用}
2,再看js的

//test.js
var addons = require('D:/hw_v8/hw_v8/hw_v8');console.log('C/C++ addons.hello() =', addons.hello());
3,这样就行了,,,,哈哈


当然还有编译

运行vs2010时,,会跳出这样一个对话框,,

这个都不重要,,,可忽略,,,O(∩_∩)O哈哈~

然后就会看到在你新建的目录里面就会有一个hw_v8.node文件

然后在就在cmd.exe运行,,,,,,♪(^∇^*),,就看到了这个

最后就看到了这个

然后就看到我最想看的“hello world!”了。。。。。。

就这么完美的解决了。。。。


当然了,,,你也可以看看这个http://iammr.7.blog.163.com/blog/static/49102699201201565822189/

0 0