Is there a better way to create objects in IScriptControl than this?
Result := SC.Eval('new Date()');
I need something like this:
function CreateJSObject(JSClassName: string; Params: PDispParams): OleVariant;
a naive implementation would be
var
S: string;
begin
S := '';
for I := P.cArgs - 1 downto 0 do
begin
if S <> '' then
S := S + ', ';
S := S + ConvertParamToJSSyntax(OleVariant(P.rgvarg[I]));
end;
Result := ScriptControl.Eval('new ' + JSClassName + '(' + S + ');');
end;
Query the IDispachEx interface on the CodeObject property of the MSScriptControl. It is a pointer on the global state of the JScript and it contains all objects added to it. Then do an InvokeEx with a DISPATCH_CONSTRUCT parameter on the object name you want to create. This would be equivalent to calling "new".
This would create an object of the correct type and you would not have to convert them to javascript types. You'll be able to pass native objects to the constructor as well.
I know that this works for constructors defined in script. I'm not sure about Date which is a native property.
This works on JScript and VBScript activescripting host, but some others scripting host does not return anything on CodeObject, so this is not very portable.