Friday, October 30, 2020

Variant support in chakracore-delphi

The new experimental/variants branch of chakracore-delphi introduces some experimental support for Variants. It's now possible to write code similar to the following:

// Calling javascript methods on variants creates temporary variant arrays for passing parameters.
// To avoid access violations after freeing the runtime, make sure all your code using Variants gets out of scope first.
procedure Test(Context: TChakraCoreContext);
var
Global, Json, MyObj: Variant;
begin
// assign javascript Global to a Variant
Global := JsValueToVariant(Context.Global);
// read its (JSON) property and assign it to another Variant
Json := Global.JSON;
// call its method, assign result to another Variant
MyObj := Json.parse('{ "name": "world" }');
// access property
Writeln(Format('Hello, %s!', [MyObj.name]));
// call another method
Writeln(Json.stringify(MyObj));
end;
procedure Main;
var
Runtime: TChakraCoreRuntime;
Context: TChakraCoreContext;
begin
Runtime := nil;
Context := nil;
try
Runtime := TChakraCoreRuntime.Create;
Context := TChakraCoreContext.Create(Runtime);
Context.Activate;
Test(Context);
finally
Context.Free;
Runtime.Free;
end;
end;
view raw test.dpr hosted with ❤ by GitHub
The example above is using Javascript built-in JSON object and its methods: parse and stringify. On Windows, it's possible to expose an Automation object to your script, using late (IDispatch) binding. An example of this is shown in the new WmiSample demo:

// ask host to create an Automation object instance
let locator = createOleObject('WbemScripting.SWbemLocator');
const computerName = '.';
const namespace = '';
const userName = '';
const password = '';
// use it (access its properties and methods) from Javascript
let service = locator.ConnectServer(computerName, namespace, userName, password);
const className = 'Win32_OperatingSystem';
console.log(`***** InstancesOf('${className}') *****`);
let items = service.InstancesOf(className);
items.forEach((item, i) => {
if (i > 0) {
console.log('');
}
item.Properties_.forEach((p) => {
console.log(`${p.Name}: ${item[p.Name]}`);
});
});
view raw wmi-example.js hosted with ❤ by GitHub
and its output:

***** InstancesOf('Win32_OperatingSystem') *****
BootDevice: \Device\HarddiskVolume1
BuildNumber: 19041
BuildType: Multiprocessor Free
Caption: Microsoft Windows 10 Pro
CodeSet: 1252
CountryCode: 1
CreationClassName: Win32_OperatingSystem
CSCreationClassName: Win32_ComputerSystem
CSDVersion: null
CSName: TLEILAX
CurrentTimeZone: 60
DataExecutionPrevention_32BitApplications: true
DataExecutionPrevention_Available: true
DataExecutionPrevention_Drivers: true
DataExecutionPrevention_SupportPolicy: 2
Debug: false
Description:
Distributed: false
EncryptionLevel: 256
ForegroundApplicationBoost: 2
FreePhysicalMemory: 11170572
FreeSpaceInPagingFiles: 2451164
FreeVirtualMemory: 13871392
InstallDate: 20200610091003.000000+060
LargeSystemCache: null
LastBootUpTime: 20201030112159.586692+060
LocalDateTime: 20201030143957.032000+060
Locale: 0409
Manufacturer: Microsoft Corporation
MaxNumberOfProcesses: -1
MaxProcessMemorySize: 137438953344
MUILanguages: en-US,de-DE,en-GB
Name: Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition2
NumberOfLicensedUsers: null
NumberOfProcesses: 171
NumberOfUsers: 2
OperatingSystemSKU: 48
Organization:
OSArchitecture: 64-bit
OSLanguage: 1033
OSProductSuite: 256
OSType: 18
OtherTypeDescription: null
PAEEnabled: null
PlusProductID: null
PlusVersionNumber: null
PortableOperatingSystem: false
Primary: true
ProductType: 1
RegisteredUser: ???????
SerialNumber: ?????-?????-?????-?????
ServicePackMajorVersion: 0
ServicePackMinorVersion: 0
SizeStoredInPagingFiles: 2490368
Status: OK
SuiteMask: 272
SystemDevice: \Device\HarddiskVolume2
SystemDirectory: C:\WINDOWS\system32
SystemDrive: C:
TotalSwapSpaceSize: null
TotalVirtualMemorySize: 19267124
TotalVisibleMemorySize: 16776756
Version: 10.0.19041
WindowsDirectory: C:\WINDOWS
view raw wmi-example.txt hosted with ❤ by GitHub
For more example code, you can also check out the associated tests.

Thursday, April 30, 2020

FastMM5 released

Check it out:

First non-beta release of FastMM version 5 (a complete rewrite) has just been made public on GitHub.