IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 八、JS使用C++类及其方法 -> 正文阅读

[C++知识库]八、JS使用C++类及其方法

????????参考《Getting started with embedding V8 · V8

Accessing dynamic variables

首先,创建Point类

class Point
{
public:
    Point( int x, int y ) : x_( x ), y_( y ) { }
    int x_, y_;
};

其次,编写Point类的创建函数:

// point constructor
void PointConstructor( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Handle<v8::Object> object = info.This();
    v8::HandleScope handle_scope( info.GetIsolate() );

    int x = info[0]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();
    int y = info[1]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();

    Point* pt = new Point( x, y );
    object->SetInternalField( 0, v8::External::New( info.GetIsolate(), pt ) );

    info.GetReturnValue().Set( object );
}

定义属性x、y的方法:

// getter setter
void GetX( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    info.GetReturnValue().Set( v8::Integer::New( info.GetIsolate(), static_cast<Point*>(ptr)->x_ ) );
}

void SetX( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    static_cast<Point*>(ptr)->x_ = info[0]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();
}

void GetY( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    info.GetReturnValue().Set( v8::Integer::New( info.GetIsolate(), static_cast<Point*>(ptr)->y_ ) );
}

void SetY( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    static_cast<Point*>(ptr)->y_ = info[0]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();
}

声明Point类在js文件中访问的名称,以及get、set方法名称:

v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New( isolate );
v8::Handle<v8::FunctionTemplate> pt_temp = v8::FunctionTemplate::New( isolate, PointConstructor );
pt_temp->SetClassName( v8::String::NewFromUtf8( isolate, "Point" ).ToLocalChecked() );
global->Set( v8::String::NewFromUtf8( isolate, "Point" ).ToLocalChecked(), pt_temp );

v8::Handle<v8::ObjectTemplate> pt_proto = pt_temp->PrototypeTemplate();

pt_proto->Set( isolate, "GetX", FunctionTemplate::New( isolate, GetX ) );
pt_proto->Set( isolate, "SetX", FunctionTemplate::New( isolate, SetX ) );
pt_proto->Set( isolate, "GetY", FunctionTemplate::New( isolate, GetY ) );
pt_proto->Set( isolate, "SetY", FunctionTemplate::New( isolate, SetY ) );
pt_temp->InstanceTemplate()->SetInternalFieldCount( 1 );

这样就可以在js文件中创建Point对象,通过get、set访问对象属性了。

main.cpp

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "include/libplatform/libplatform.h"
#include "include/v8.h"

#include "point.h"

using namespace std;
using namespace v8;

const std::string fileName = "file.js";

// point constructor
void PointConstructor( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Handle<v8::Object> object = info.This();
    v8::HandleScope handle_scope( info.GetIsolate() );

    int x = info[0]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();
    int y = info[1]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();

    Point* pt = new Point( x, y );
    object->SetInternalField( 0, v8::External::New( info.GetIsolate(), pt ) );

    info.GetReturnValue().Set( object );
}

// getter setter
void GetX( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    info.GetReturnValue().Set( v8::Integer::New( info.GetIsolate(), static_cast<Point*>(ptr)->x_ ) );
}

void SetX( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    static_cast<Point*>(ptr)->x_ = info[0]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();
}

void GetY( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    info.GetReturnValue().Set( v8::Integer::New( info.GetIsolate(), static_cast<Point*>(ptr)->y_ ) );
}

void SetY( const v8::FunctionCallbackInfo<v8::Value>& info ) {
    v8::Local<v8::Object> self = info.Holder();
    v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast( self->GetInternalField( 0 ) );
    void* ptr = wrap->Value();

    static_cast<Point*>(ptr)->y_ = info[0]->Int32Value( info.GetIsolate()->GetCurrentContext() ).ToChecked();
}


// Reads a file into a v8 string.
MaybeLocal<String> ReadFile( Isolate* isolate, const string& name ) {
    FILE* file = fopen( name.c_str(), "rb" );
    if ( file == NULL ) return MaybeLocal<String>();

    fseek( file, 0, SEEK_END );
    size_t size = ftell( file );
    rewind( file );

    std::unique_ptr<char> chars( new char[size + 1] );
    chars.get()[size] = '\0';
    for ( size_t i = 0; i < size;) {
        i += fread( &chars.get()[i], 1, size - i, file );
        if ( ferror( file ) ) {
            fclose( file );
            return MaybeLocal<String>();
        }
    }
    fclose( file );
    MaybeLocal<String> result = String::NewFromUtf8(
        isolate, chars.get(), NewStringType::kNormal, static_cast<int>(size) );
    return result;
}

int main( int argc, char* argv[] ) {
    // Initialize V8.
    v8::V8::InitializeICUDefaultLocation( argv[0] );
    v8::V8::InitializeExternalStartupData( argv[0] );
    std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
    v8::V8::InitializePlatform( platform.get() );
    v8::V8::Initialize();

    // Create a new Isolate and make it the current one.
    v8::Isolate::CreateParams create_params;
    create_params.array_buffer_allocator =
        v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    v8::Isolate* isolate = v8::Isolate::New( create_params );
    {
        v8::Isolate::Scope isolate_scope( isolate );

        // Create a stack-allocated handle scope.
        v8::HandleScope handle_scope( isolate );

        v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New( isolate );
        v8::Handle<v8::FunctionTemplate> pt_temp = v8::FunctionTemplate::New( isolate, PointConstructor );
        pt_temp->SetClassName( v8::String::NewFromUtf8( isolate, "Point" ).ToLocalChecked() );
        global->Set( v8::String::NewFromUtf8( isolate, "Point" ).ToLocalChecked(), pt_temp );

        v8::Handle<v8::ObjectTemplate> pt_proto = pt_temp->PrototypeTemplate();

        pt_proto->Set( isolate, "GetX", FunctionTemplate::New( isolate, GetX ) );
        pt_proto->Set( isolate, "SetX", FunctionTemplate::New( isolate, SetX ) );
        pt_proto->Set( isolate, "GetY", FunctionTemplate::New( isolate, GetY ) );
        pt_proto->Set( isolate, "SetY", FunctionTemplate::New( isolate, SetY ) );

        pt_temp->InstanceTemplate()->SetInternalFieldCount( 1 );

        // Create a new context.
        v8::Local<v8::Context> context = v8::Context::New( isolate, nullptr, global );

        // Enter the context for compiling and running the hello world script.
        v8::Context::Scope context_scope( context );

        {
            // Create a string containing the JavaScript source code.
            v8::Local<v8::String> source;
            if ( !ReadFile( isolate, fileName ).ToLocal( &source ) ) {
                fprintf( stderr, "Error reading '%s'.\n", fileName.c_str() );
                return -1;
            }

            // Compile the source code.
            v8::Local<v8::Script> script =
                v8::Script::Compile( context, source ).ToLocalChecked();

            // Run the script to get the result.
            v8::Local<v8::Value> result = script->Run( context ).ToLocalChecked();

            // Convert the result to an UTF8 string and print it.
            v8::String::Utf8Value utf8( isolate, result );
            printf( "%s\n", *utf8 );
        }
    }

    // Dispose the isolate and tear down V8.
    isolate->Dispose();
    v8::V8::Dispose();
    v8::V8::ShutdownPlatform();
    delete create_params.array_buffer_allocator;
    return 0;
}

point.h

#pragma once

class Point
{
public:
    Point( int x, int y ) : x_( x ), y_( y ) { }
    int x_, y_;
};

?

file.js

function point() {
    var pt = new Point(10, 15);

    pt.SetX(20);
    pt.SetY(30);

    return new Array(pt.GetX(), pt.GetY());
}

point();

运行结果:

?

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-17 15:11:24  更:2021-08-17 15:11:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 0:56:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码