DukTape JS

Author: Dave
Date: 06.05.15 - 7:38pm

I have been tinkering with a new project, to try and get the dukTape js engine working with visual basic six. My end goal hopefully would be similar to what I did with script basic.

Since JavaScript supports obj.method notation, this time around I want to up the ante and be able to call methods on com objects the same way you can in the Microsoft script control.

So how do we map an arbitrary number and prototypes of functions and wire them into the correct method on the COM object.

When you call AddObject in the Microsoft script control, it has to use the typelib information to extract all of the prototypes to know how to call them. Ok I have done this.

But how do you create the necessary JavaScript prototypes and wire them all up to send them to the correct com method?

in the duktape C api you can create arbitrary objects and methods on them..but they are designed to each call a specific C function. This wont work for what I am trying.

Here is what I came up with so far. it is working, but will be further refined.

I added a single native C function called resolver.

int comResolver(duk_context *ctx) {
	int i, retType ;
	const char* meth = 0;

	int n = duk_get_top(ctx);  /* #args */

	if(n < 0) return 0;
	if(vbHostResolver==NULL) return 0; 
	meth = duk_safe_to_string(ctx, 0); //first arg is obj.method string
	retType = vbHostResolver(meth, strlen(meth), ctx, n-1);

	return 1;  

then i built up a javascript class source file (manually for now)

function dlgClass(){
	this.ShowOpen = function(filt,initDir,title,hwnd){ 
		return resolver("call:cmndlg:OpenDialog:long:[string]:[string]:[long]:r_string", filt,initDir,title,hwnd); 

function fsoClass(){
	this.ReadFile = function(fname){
		return resolver("call:fso:ReadFile:string:r_string", fname); 	

var cmndlg = new dlgClass();
var fso = new fsoClass();

var form = {
  set caption (str) {
    resolver("let:form:caption:string", str); 
  get caption() {
    return resolver("get:form:caption:string"); 
  ReadFile : function(fname){
		return resolver("call:fso:ReadFile:string:r_string", fname); 	
  ShowOpen : function(filt,initDir,title,hwnd){ 
		return resolver("call:cmndlg:OpenDialog:long:[string]:[string]:[long]:r_string", filt,initDir,title,hwnd); 


Now in vb6 I can do the following (all testing and working so far) to make alert() work I rewired the DUK_FWRITE to redirect to my own stdout handler which calls back to vb6

rv = Eval("1+2") 'works
rv = Eval("alert(1+2)") 'works
rv = Eval("pth = cmndlg.ShowOpen(4,'title','c:',0); alert(fso.ReadFile(pth))") 'works
rv = Eval("form.caption = 'test!'; alert(form.ReadFile('c:lastGraph.txt'));")
rv = Eval("form.caption = 'test!';alert(form.caption)")

So everything is looking good so far. property gets and sets are functional as are arbitrary method calls. To make the magic work my vb host resolver function
  1. looks up the live object from the name
  2. extracts method/property name
  3. loads a variant array with the relevant args/values/types
  4. uses TLIApplication.InvokeHook to call the method
  5. sets the return value
  6. returns execution back to the script.
So with the initial experimentation stage up and running, next step will be to use the typelib info to parse the COM objects on the fly when I call AddObject and build the appropriate js class file for each, and well as save the method info for the resolver.

I will probably save the method info in a class in parsed format and just use a methodID in the javascript file instead of a full text based prototype which is bulky but good for initial testing.

Before I goto much farther down that road which will be a lot of work though..I think I will start experimenting with the debugger capabilities (here and here) to make sure I can control it.

just having a more modern Javascript implementation available for VB6 is already a win, COM object support was a must have though. but if I can build a full IDE for it like i did for script basic..then that will knock it out of the park.

One more thing to resolve..right now all the COM objects and methods I have tested are on static global top level objects. What if a method returns a new object instance such as with the Microsoft Scripting runtime or Excel automation? ex:

Function OpenTextFile(FileName As String, 
	[IOMode As IOMode = ForReading], 
	[Create As Boolean = False], 
	[Format As Tristate = TristateFalse]
) As TextStream

Function ReadAll() As String Member of Scripting.TextStream

how to call a method on a specific instance of an object instead of static top level global objects like we have been? My first thought was to add a int field to the TextStreamClass of my own that would hold an objptr(obj) to identify it uniquely. Resolver will have to return a new class instance, and set say a hInst field of it. Thanks to some help from the guys on the IRC channel this is already working in a simple test scenario ex:

duk_get_global_string(ctx, "fsoClass"); 
duk_new(ctx, 0);

Also looking at the debugger docs, they are marked as highly experimental. My instinct is telling me to hold off on messing with the debugger side of it for a bit just in case.

Comments: (0)

Leave Comment:
Email: (not shown)
Message: (Required)
Math Question: 86 + 87 = ? followed by the letter: H 

About Me
More Blogs
Main Site
Posts: (year)
2024 (2)
     ffmpeg voodoo
     RegJump Vb
2023 (9)
     VB6 Virtual Files
     File handles across dlls
     python abort / script timeout
     VB6 Python embed w/debugger
     python embedding
     VB6 IDE Enhancements
     No Sleep
     A2W no ATL
2022 (4)
     More VB6 - C data passing
     Vb6 Asm listing
     Byte Array C to VB6
     Planet Source Code DVDs
2021 (2)
     Obscure VB
     VB6 IDE SP6
2020 (4)
     BSTR from C Dll to VB
     Cpp Memory Manipulation
     ActiveX Binary Compatability
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 (6)
     Python and VB6
     Python pros and cons
     download web Dir
     vc rand in python
     VB6 Language Enhancement
     Register .NET as COM
2016 (22)
     VB6 CDECL
     UDT Tricks pt2
     Remote Data Extraction
     Collection Extender
     VB6 FindResource
     DirList Single Click
     Reset CheckPoint VPN Policy
     VB6 BSTR Oddities Explained
     SafeArrays in C
     BSTR and Variant in C++
     Property let optional args
     Misc Libs
     Enum Named Pipes
     Vb6 Collection in C++
     VB6 Overloaded Methods
     VB6 Syncronous Socket
     Simple IPC
     VB6 Auto Resize Form Elements
     Mach3 Automation
     Exit For in While
2015 (15)
     C# self register ocx
     VB6 Class Method Pointers
     Duktape Debug Protocol
     QtScript 4 VB
     Vb6 Named Args
     vb6 Addin Part 2
     VB6 Addin vrs Toolbars
     OpenFile Dialog MultiSelect
     Duktape Example
     DukTape JS
     VB6 Unsigned
     .Net version
     TitleBar Height
     .NET again
     VB6 Self Register OCXs
2014 (25)
     Query Last 12 Mos
     Progid from Interface ID
     VB6 to C Array Examples
     Human Readable Variant Type
     ScriptBasic COM Integration
     CodeView Addin
     ScriptBasic - Part 2
     Script Env
     MSCOMCTL Win7 Error
     printf override
     History Combo
     Disable IE
     API Hooking in VB6
     Addin Hook Events
     FastBuild Addin
     VB6 MemoryWindow
     Link C Obj Files into VB6
     Vb6 Standard Dlls
     CStr for Pascal
     Lazarus Review
     asprintf for VS
     VB6 GlobalMultiUse
     Scintilla in VB6
     Dynamic Highlight
     WinVerifyTrust, CryptMsgGetParam VB6
2013 (4)
     MS GLEE Graphing
     printf for VB6
     C# App Config
     Tero DES C# Test
2012 (10)
     VC 2008 Bit Fields
     Speed trap
     C# Db Class Generator
     VB6 vrs .NET (again)
     FireFox Whois Extension
     git and vb6
     Code Additions
     Compiled date to string
     C# ListView Sorter
     VB6 Wish List
2011 (7)
     C# Process Injection
     CAPTCHA Bots
     C# PE Offset Calculator
     VB6 Async Download
     Show Desktop
     coding philosophy
     Code release
2010 (11)
     Dll Not Found in IDE
     Advanced MSScript Control
     random tip
     Clipart / Vector Art
     VB6 Callback from C#
     Binary data from VB6 to C#
     CSharp and MsScriptControl
     HexDumper functions
     Js Beautify From VB6 or C#
     vb6 FormPos
     Inline Asm w VB6
2009 (3)
     The .NET Fiasco
     One rub on computers
     Universal extractor