[VBA/VB6] COM prototyping and monkey patching without dlls
For those who daily struggle against the limitations imposed by VBA/VB6 regarding class inheritance, polymorphism, and advanced methods for arrays and objects, as well as for those who have found areas of code that could be handled more elegantly and concisely with more expressive syntax, and for those who have dreamed of safely modifying the behavior of certain DLLs at runtime without code injection or enforced memory protection, today I present the Advanced Scripting Framework (ASF), an abstract runtime layer that even includes its own independent regular expression engine.
Below is an example of what is possible to achieve in a limited language like VBA.
Code:
// prototypes.vas
export prototype.COM.Range addStyle(color) {
this.Interior.Color = color;
};
export prototype.COM.Worksheet highlight(rng, color) {
rng.addStyle(color);
};
// main_prototype.vas
scwd(wd);
import { Range_addStyle, Worksheet_highlight } from './prototypes.vas';
// Prototypes are live immediately after import
let ws = $1.ActiveSheet;
let rng = ws.Range('J1:L3');
rng.addStyle(65535); // yellow
ws.highlight(rng, 255); // red
return rng.Interior.Color
Here is the driving VBA code:
Code:
Private Sub module_system_prototype_imports()
Dim result As Long
Dim wd As String
Dim eng As New ASF
wd = ThisWorkbook.path
With eng
.AppAccess = True
.InjectVariable "wd", wd
result = CLng(.Execute(wd & "\main_prototype.vas", ThisWorkbook))
End With
'Expected: 255
End Sub
And here is the execution trace:
Code:
=== Runtime Log ===
RUN Program:
CALL: ActiveSheet() -> <Worksheet>
CALL: range('J1:L3') -> <Range>
CALL: addstyle(65535) ->
CALL: __PROTOTYPE_RANGE_ADDSTYLE(65535) ->
CALL: Interior() -> <Interior>
CALL: highlight(<Range>, 255) ->
CALL: __PROTOTYPE_WORKSHEET_HIGHLIGHT(<Range>, 255) ->
CALL: addstyle(255) ->
CALL: __PROTOTYPE_RANGE_ADDSTYLE(255) ->
CALL: Interior() -> <Interior>
CALL: Interior() -> <Interior>
CALL: Color() -> 255
CALL: @anon() -> 255
ASF comes with 233 Ruberduck tests and its regex engine has over 190 tests. Community testing is now welcome!