CN108874374A - A kind of script engine interface abstraction layer and its application method - Google Patents

A kind of script engine interface abstraction layer and its application method Download PDF

Info

Publication number
CN108874374A
CN108874374A CN201810515982.6A CN201810515982A CN108874374A CN 108874374 A CN108874374 A CN 108874374A CN 201810515982 A CN201810515982 A CN 201810515982A CN 108874374 A CN108874374 A CN 108874374A
Authority
CN
China
Prior art keywords
module
script
class
engine
call back
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201810515982.6A
Other languages
Chinese (zh)
Inventor
陈建华
凌华彬
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Xiamen Yaji Software Co Ltd
Original Assignee
Xiamen Yaji Software Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Xiamen Yaji Software Co Ltd filed Critical Xiamen Yaji Software Co Ltd
Priority to CN201810515982.6A priority Critical patent/CN108874374A/en
Publication of CN108874374A publication Critical patent/CN108874374A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)
  • Stored Programmes (AREA)

Abstract

The present invention proposes script engine interface abstraction layer and its application method, it is related to computer software fields, including macroblock, script and C++ object mapping table, ScriptEngine module, Value module, AutoHandleScope module, Class module, State module, Object module, HandleObject module.The present invention can easily be adapted to different JavaScript even other non-JavaScript script engines, while guaranteeing that the performance called between scripting language and primary language does not decline, accomplishes easily to switch or upgrade bottom script engine and do not need to modify any binding code;Design is more efficient, and code, which calls the calling to C++ layers of user from script layer, to be determined during binding code compilation by macro mode;Abstraction layer interface uses object oriented designing, and interface is succinct, and the institute that satisfaction writes binding code needs is functional, supports the JavaScript script engine of mainstream on the market.

Description

A kind of script engine interface abstraction layer and its application method
Technical field
The present invention relates to computer software fields, especially a kind of script engine interface abstraction layer and its application method.
Background technique
Script binding technology refers to the class and side that the interface provided by script engine writes primary language (C or C++) Method is registered to a kind of technology that script layer is called for script.On the market the JavaScript script engine of mainstream have V8, JavaScriptCore, SpiderMonkey, ChakraCore, they are Chrome, Safari, Firefox, Microsoft respectively The embedded JavaScript script engine of Edge browser.The C++ interface that different script engines provide is different or even same A script engine provides C++ interface there is also difference in different versions, this binding code for causing C++ to interact with script is also deposited In very big difference.Based on a kind of binding code that script engine interface is write other feet can not be switched to by simply configuring This engine, the different editions of same script engine are also likely to be present interface compatibility problem and lead to not light upgrade script engine.
It applies when one when different platform needs different script engines, for example uses V8 engine, apple in Android mobile phone The JavaScriptCore engine that fruit mobile phone uses system to embed, prior art have:
1, it is respectively that different platform using different script engines writes different binding code, that is, needs more parts of bindings Code, maintenance cost is double, can not switch bottom engine.Online wechat trivial games use this scheme at the beginning of 2018.
2, some C++ type write and method are registered to by the open source library of this industry of NodeJS prevalence using V8 In JavaScript script layer, developer can by be easier to using JavaScript scripting language's complexity client With server side logic.The bottom of NodeJS by node-chakracore project success is cut by ChakraCore team of Microsoft It is changed to ChakraCore.Mozilla company also uses is by NodeJS underlay handovers similar to scheme (spidernode) SpiderMonkey script engine.The scheme of this type needs to realize a set of adaptation layer, and the interface and V8 engine of adaptation layer provide Interface it is completely the same, the institute of V8 is adapted to using the function that the non-V8 engine such as ChakraCore or SpiderMonkey provides There is interface.The shortcomings that this scheme is:1) repertoire with non-V8 engine implementation V8 engine is needed, realizes that logic is more complex. 2) author for binding code needs to understand in depth the interface that complicated V8 engine provides, and learning cost is higher.3) it does not provide just Prompt method is switched fast script engine.
3,Node N-API:Since NodeJS 8.0, Node official provides a set of N-API, and N-API is a set of abstract Layer interface, this abstraction layer interface do not change with the variation of V8 engine interface, and the developer of Node plug-in unit is write using this API Without modified because of Node upgraded version binding code and recompilate plug-in unit, i.e., plug-in unit compatibility than NodeJS 8.0 it Preceding version is higher.The shortcomings that this scheme is:1) level of abstraction has only been adapted to V8 engine, can not temporarily be adapted to other script engines;2) Level of abstraction solves the problems, such as it is plug-in unit upgrade problem, is not the scheme of optimal performance, i.e. the calling needs of script interface pass through One layer identification code transfer is just capable of calling the call back function that user writes, and call overhead is larger.
Summary of the invention
One aspect of the present invention provides a kind of script engine interface abstraction layer, including macroblock, script and C++ object map Table, ScriptEngine module, Value module, AutoHandleScope module, Class module, State module, Object Module, HandleObject module, wherein
The macroblock for solving the problems, such as that different script engine call back function parameter types and return value are different types of, Regardless of what engine bottom uses, developer uses the definition mode of same call back function, after having defined call back function, needs Use macro the packing to call back function of SE_BIND_XXX series;
The script and C++ object mapping table are for inquiring the corresponding scripting object of C++ pointer to object acquisition;
The ScriptEngine module is used to administer the initialization of script engine, destroys, restarts, C++ Module registers, adding Script is carried, the cleaning extremely of garbage reclamation, script is forced and whether enables debugger;
The Value module is reference of the script variable at C++ layers;
The Object module saves the weak reference to scripting object;
The HandleObject module is an assisted class, is created manually for managing in Object module simplerly Build release, root and the unroot operation of object;
The Class module after the creation of Class type, does not need manual releasing for exposing C++ class into script environment Memory, committed memory can be abstracted layer and automatically process;
The AutoHandleScope module only has realization, other script engines of level of abstraction adaptation on V8 engine It is at present all that empty real shows this type;
The State module can be obtained currently as a context object in binding readjustment by State module C++ pointer to object, se::Object pointer to object, parameter list, return value reference.
Another aspect of the present invention provides a kind of application method of script engine interface abstraction layer, setting script relevant operation C++ call back function is associated with C++ object in call back function, and specific step is as follows:
Step 1:Obtain namepace object;
Step 2:Create se::Class;
Step 3:For se::Class defined attribute and method;
Step 4:Register se::Class is into script virtual machine;
Step 5:Obtain se::The proto object of Class;
Step 6:The static attribute and static method of binding class are set by proto object;
Step 7:Empty exception.
A kind of script engine interface abstraction layer and its application method provided by the invention, the beneficial effect is that:It can be light Different JavaScript even other non-JavaScript script engines are adapted to, guarantee scripting language and primary language (C/C+ +) between the performance called while do not decline, accomplish easily to switch or upgrading bottom script engine and do not need to modify any Bind code;Level of abstraction design of the invention is more efficient, and it is by macro that code, which calls the calling to C++ layers of user from script layer, Mode during binding code compilation determine, there is no transfer distributions for this process;Abstraction layer interface uses object-oriented Design, it includes ScriptEngine, Class, Value, Object, HandleObject, State that interface is succinct, AutoHandleScope these types data type module, the institute that satisfaction writes binding code needs is functional, and support is led on the market The JavaScript script engine of stream.
Detailed description of the invention
Fig. 1 is script engine interface abstraction layer module map of the invention;
Fig. 2 is the application framework figure of script engine interface abstraction layer of the invention;
Fig. 3 is the applicating flow chart of script engine interface abstraction layer of the invention.
Specific embodiment
To further illustrate that each embodiment, the present invention are provided with attached drawing.These attached drawings are that the invention discloses one of content Point, mainly to illustrate embodiment, and the associated description of specification can be cooperated to explain the operation principles of embodiment.Cooperation ginseng These contents are examined, those of ordinary skill in the art will be understood that other possible embodiments and advantages of the present invention.In figure Component be not necessarily to scale, and similar component symbol is conventionally used to indicate similar component.
Now in conjunction with the drawings and specific embodiments, the present invention is further described.
As shown in Figure 1, script engine interface abstraction layer provided in this embodiment, including macroblock, script and C++ object reflect Firing table, ScriptEngine module, Value module, AutoHandleScope module, Class module, State module, Object module, HandleObject module, wherein
Macroblock for solving the problems, such as that different script engine call back function parameter types and return value are different types of, regardless of Bottom use what engine, developer use same call back function definition mode, after having defined call back function, need using Macro call back function is packed of SE_BIND_XXX series.Script engine level of abstraction provides following several macro:
SE_BIND_PROP_GET is macro:Pack the call back function that scripting object attribute is read;
SE_BIND_PROP_SET is macro:Pack the call back function of scripting object attribute write-in;
SE_BIND_FUNC is macro:Script function is packed, can be used for overall situation function, class members's function, class static function;
SE_DECLARE_FUNC is macro:Statement script function generally uses in .h header file;
SE_BIND_CTOR is macro:Pack script constructed fuction;
SE_BIND_SUB_CLS_CTOR is macro:Pack the constructed fuction of script subclass;
SE_FINALIZE_FUNC is macro:Scripting object is packed by the call back function after garbage reclamation;
SE_DECLARE_FINALIZE_FUNC is macro:Statement script object is by the call back function after garbage reclamation;
_ SE is macro:For packing the title of call back function, the general readjustment based on level of abstraction that developer can be write Function escape is the definition for the call back function that each script engine can identify.
Script and C++ object mapping table are for inquiring the corresponding scripting object of C++ pointer to object acquisition.
ScriptEngine module is the administrator of script engine, for administering the initialization of script engine, destruction, again It opens, C++ Module registers, load script, force the cleaning extremely of garbage reclamation, script and whether enable debugger.
Value module be reference of the script variable at C++ layers, the script variable type of JavaScript have object, This six seed type of number, string, boolean, null, undefined, therefore Value module includes using union These four have Value Types by object, number, string, boolean, and no Value Types null, undefined can be by Value moulds In block _ type variable directly indicates, if what is saved in Value module is base data type, such as number, string Or boolean, Value inside modules are a value copy of direct storage, internal members' cloth of the Value module of the present embodiment Office is as follows:
Object module saves the weak reference to scripting object, and using weak reference the reason of is as follows:
1, the needs of the life cycle of scripting object control C++ object, when passing through similar varsp=new in script layer cc.Sprite("a.png");Method create a bound object after, construction call back function binding in we will create One Object module is simultaneously retained in a global mapping table;
2, weak reference is more flexible, but can also be by calling root method to support strongly reference manually, and root can be script pair As being put into the region that do not scanned by Garbage Collector, after calling root, Object module is with regard to strongly reference scripting object, only Have after unroot is called or Object module is released, scripting object can just be taken back to and be scanned by Garbage Collector The region arrived.
Object module also supports the manual wound of following several unbundling object types in addition to supporting to create bound object It builds:
Plain Object:Pass through se::Object::CreatePlainObject creation, similar in JavaScript Var a={ };
Array Object:Pass through se::Object::CreateArrayObject creation, similar in JavaScript Var a=[];
Typed Array Object:Pass through se::Object::CreateTypedArray creation, it is similar Var a=new Uint8Array (buffer) in JavaScript;
Array Buffer Object:Pass through se::Object::CreateArrayBufferObject, it is similar Var a=new ArrayBuffer (len) in JavaScript.
HandleObject module is an assisted class, for managing manual creation pair in Object module simplerly As, root and unroot operation.
Class module after the creation of Class type, does not need in manual releasing for exposing C++ class into script environment It deposits, layer can be abstracted and automatically processed, wherein Class type creation method is as follows:
static se::Class*create(className,obj,parentProto,ctor):Creation one Class after succeeding in registration, can pass through var xxx=new SomeClass () in JavaScript layers;Mode create One object;
bool defineFunction(name,func):Define the member function in Class;
bool defineProperty(name,getter,setter):Define Class attribute reader;
bool defineStaticFunction(name,func):The static member function for defining Class, can pass through The mode of this non-new of SomeClass.foo () accesses, unrelated with class instance objects;
bool defineStaticProperty(name,getter,setter):Define the static attribute read-write of Class Device, can be unrelated with class instance objects by SomeClass.propertyA direct read/write;
bool defineFinalizeFunction(func):Scripting object is defined to be adjusted back by the C++ after garbage reclamation;
bool install():It registers such into script virtual machine;
Object*getProto():Obtain the prototype object for the class being registered in script, similar functionFoo The Foo.prototype of () { };
const char*getName()const:Obtain the title of current Class.
AutoHandleScope module, only there is realization on V8 engine, and other script engines of level of abstraction adaptation are current All it is that empty real shows this type, is because needing to trigger script relevant operation in C++ function when having, for example adjust in V8 engine With script function, any calling v8 such as script attribute are accessed::Local<>Operation, V8 mandatory requirement call these operation before There must be a v8::HandleScope scope, otherwise can initiator collapse.In the execution of any C++ code, need Before the logic for calling JavaScript, an AutoHandleScope module is stated, such as:
State module can obtain current C+ by State module as a context object in binding readjustment + pointer to object, se::Object pointer to object, parameter list, return value reference.
As shown in Fig. 2, script engine interface abstraction layer provided in this embodiment, as different script engines and uses script A bridge between the game or application that language is write, the level of abstraction of the present embodiment are based on independent of particular script engine The script binding code that this level of abstraction is write needs not move through any modification, and bottom only can be realized by a simple macro configuration Layer script engine easily switches or upgrades.
The definition of tetra- kinds of script engine bound functions of JavaScriptCore, SpiderMonkey, V8, ChakraCore is each It is not identical, it is specific as follows:
As shown in figure 3, largely work is setting to the application method of script engine interface abstraction layer provided in this embodiment The C++ call back function of script relevant operation, is associated with C++ object in call back function, and specific binding method is as follows:
Step 1:Obtain namepace object;
Step 2:Create se::Class;
Step 3:For se::Class defined attribute and method;
Step 4:Register se::Class is into script virtual machine;
Step 5:Obtain se::The proto object of Class;
Step 6:The static method and static attribute of binding class are set by proto object;
Step 7:Empty exception.
It is even other non-can be easily adapted to different JavaScript for script engine interface abstraction layer provided by the invention JavaScript script engine is done while guaranteeing that the performance called between scripting language and primary language (C/C++) does not decline To light switching or upgrading bottom script engine and do not need to modify any binding code;Level of abstraction design of the invention is more Efficiently, code, which calls the calling to C++ layers of user from script layer, is determined during binding code compilation by macro mode, There is no transfer distributions for this process;Abstraction layer interface uses object oriented designing, and interface is succinct, includes ScriptEngine, Class, Value, Object, HandleObject, State, AutoHandleScope these types data Type block, the institute that satisfaction writes binding code needs is functional, supports the script engine of mainstream on the market.
Although specifically showing and describing the present invention in conjunction with preferred embodiment, those skilled in the art should be bright It is white, it is not departing from the spirit and scope of the present invention defined by the appended claims, it in the form and details can be right The present invention makes a variety of changes, and is protection scope of the present invention.

Claims (2)

1. a kind of script engine interface abstraction layer, which is characterized in that including macroblock, script and C++ object mapping table, ScriptEngine module, Value module, AutoHandleScope module, Class module, State module, Object mould Block, HandleObject module, wherein
The macroblock for solving the problems, such as that different script engine call back function parameter types and return value are different types of, regardless of Bottom use what engine, developer use same call back function definition mode, after having defined call back function, need using Macro call back function is packed of SE_BIND_XXX series;
The script and C++ object mapping table are for inquiring the corresponding scripting object of C++ pointer to object acquisition;
The ScriptEngine module is used to administer the initialization of script engine, destroys, restarts, C++ Module registers, load foot Originally, force the cleaning extremely of garbage reclamation, script and whether enable debugger;
The Value module is reference of the script variable at C++ layers;
The Object module saves the weak reference to scripting object;
The HandleObject module is an assisted class, for managing manual creation pair in Object module simplerly Release, root and the unroot operation of elephant;
The Class module after the creation of Class type, does not need in manual releasing for exposing C++ class into script environment It deposits, committed memory can be abstracted layer and automatically process;
The AutoHandleScope module, only there is realization on V8 engine, and other script engines of level of abstraction adaptation are current It is all that empty real shows this type;
The State module can obtain current C+ by State module as a context object in binding readjustment + pointer to object, se::Object pointer to object, parameter list, return value reference.
2. a kind of a kind of application method of script engine interface abstraction layer as described in claim 1, which is characterized in that setting foot The C++ call back function of this relevant operation is associated with C++ object in call back function, and specific step is as follows:
Step 1:Obtain namepace object;
Step 2:Create se::Class;
Step 3:For se::Class defined attribute and method;
Step 4:Register se::Class is into script virtual machine;
Step 5:Obtain se::The proto object of Class;
Step 6:The static attribute and static method of binding class are set by proto object;
Step 7:Empty exception.
CN201810515982.6A 2018-05-25 2018-05-25 A kind of script engine interface abstraction layer and its application method Pending CN108874374A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810515982.6A CN108874374A (en) 2018-05-25 2018-05-25 A kind of script engine interface abstraction layer and its application method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810515982.6A CN108874374A (en) 2018-05-25 2018-05-25 A kind of script engine interface abstraction layer and its application method

Publications (1)

Publication Number Publication Date
CN108874374A true CN108874374A (en) 2018-11-23

Family

ID=64333743

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810515982.6A Pending CN108874374A (en) 2018-05-25 2018-05-25 A kind of script engine interface abstraction layer and its application method

Country Status (1)

Country Link
CN (1) CN108874374A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109857478A (en) * 2019-01-31 2019-06-07 北京小米移动软件有限公司 Operation method, device and the readable storage medium storing program for executing of destination application
CN112083897A (en) * 2020-07-30 2020-12-15 济南浪潮高新科技投资发展有限公司 Signal declaration system, method, equipment and medium in digital logic design

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2001067241A1 (en) * 2000-03-06 2001-09-13 Conita Technologies, Inc. Virtual assistant engine
US20130339841A1 (en) * 2012-06-14 2013-12-19 Open Text S.A. Systems and methods of a script generation engine
CN105723341A (en) * 2013-06-06 2016-06-29 微软技术许可有限责任公司 Interface for representing bindings between objects in a web browser's layout engine memory space and objects in a scripting engine memory space

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2001067241A1 (en) * 2000-03-06 2001-09-13 Conita Technologies, Inc. Virtual assistant engine
US20130339841A1 (en) * 2012-06-14 2013-12-19 Open Text S.A. Systems and methods of a script generation engine
CN105723341A (en) * 2013-06-06 2016-06-29 微软技术许可有限责任公司 Interface for representing bindings between objects in a web browser's layout engine memory space and objects in a scripting engine memory space

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
幼儿园大班班长: "JSB 2.0 绑定教程,抽象层", 《博客园》 *

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109857478A (en) * 2019-01-31 2019-06-07 北京小米移动软件有限公司 Operation method, device and the readable storage medium storing program for executing of destination application
CN109857478B (en) * 2019-01-31 2022-03-25 北京小米移动软件有限公司 Target application program running method and device and readable storage medium
CN112083897A (en) * 2020-07-30 2020-12-15 济南浪潮高新科技投资发展有限公司 Signal declaration system, method, equipment and medium in digital logic design

Similar Documents

Publication Publication Date Title
CN102411506B (en) Java-system business-unit plug-in type managing system and dynamic business-function changing method
KR100712767B1 (en) Techniques for reducing the cost of dynamic class initialization checks in compiled code
CN104793946B (en) Dispositions method and system are applied based on cloud computing platform
US8799875B2 (en) Streamlining unit testing through hot code swapping
KR101318313B1 (en) Method for dynamically linking program on embedded platform and a computer readable storage medium recording an embedded platform
Pina et al. Rubah: DSU for Java on a stock JVM
US20030212983A1 (en) Method and system for modifying executable code to add additional functionality
CN105487885B (en) Hand swims hot update method
CN109491695A (en) A kind of increment updating method of integrated Android application
CN110275722A (en) Method, apparatus, equipment and storage medium for upgrade application
WO2014176587A2 (en) Systems and methods for mobile applications
KR20130069555A (en) Virtual application extension points
CN102364442B (en) Method for transplanting Wine from x86 to advanced risc machine (ARM) platform
CA2743568A1 (en) Transforming user script code for debugging
CN112835975B (en) Method for deploying, updating and calling intelligent contracts in blockchain
CN103309800A (en) Automatic webpage testing method and system
CN108874374A (en) A kind of script engine interface abstraction layer and its application method
CN114588638A (en) Method for developing game based on Lua language and program development device
CN113238819A (en) Dynamic loading method and system of drive file suitable for U-Boot
CN103176838A (en) Method and system compatible with Android native program
CN102364433A (en) Method for realizing Wine construction tool transplanting on ARM (Advanced RISC Machines) processor
CN102253884B (en) The method of simulating operating environment of single-plate software and device
WO2022120640A1 (en) Electron-based updating method and system
US9841982B2 (en) Locating import class files at alternate locations than specified in classpath information
CN109388416A (en) A kind of plug-in unit realization method and system of application software

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20181123