#1 2015-08-11 20:19:56

jf
Member
Registered: 2015-08-11
Posts: 2

Javascript strings args format in delphi callbacks

Salut Arnaud,

first I want to say that I started using your mORMot framework recently and it really is a brilliant framework. Congrat and many thanks for making-it available for all of us to use.

I'm at the point of adding client-side javascript support and I don't understand the string format coming from js args, when the argument is hardcoded. Take a look at this very simple program :

procedure TForm1.Button1Click(Sender: TObject);
var
  content: variant;
  engine: TSMEngine;
begin
  fSMManager := TSMEngineManager.Create;
  fSMManager.OnNewEngine := DoOnNewEngine;

  engine := fSMManager.ThreadSafeEngine;

  engine.MaybeGarbageCollect;

  content := engine.Global.showDownRunner('D:\temp\devCliTest\1001\showDownRunner.js');

  if Engine.ErrorExist then
    Memo1.Lines.Add(String(Engine.LastErrorMsg))
  else
    Memo1.Lines.Add(String(content));
end;

procedure TForm1.DoOnNewEngine(const Engine: TSMEngine);
var
  sVal: String;
  showDownRunner: SynUnicode;
begin
  sVal := 'function showDownRunner(pathToFile){';
  sVal := sVal + 'if (global.isFile(pathToFile)) return "File found"; else return "File not found";}';

  showDownRunner := StringToSynUnicode(sVal);

  try
    Engine.Evaluate(showDownRunner, 'showTestRunner.js');
    Engine.RegisterMethod(Engine.GlobalObj, 'isFile', isFile, 1);
  except on E:ESMException do
    Memo1.Lines.Add(e.message);
  end;
end;

function TForm1.isFile(const This: variant; const Args: array of variant): variant;
var
  fName: String;
begin
  if length(Args)<>1 then
    raise Exception.Create('Nombre d''argument incomplet pour la fonction isFile(): requis 1 (file name)');

  fName := Args[0];

  result := FileExists(fName);
end;

If I run this everything is fine. Then if I substitute the argument in the javascript function with an hardcoded value instead of pathToFile as per :

  sVal := sVal + 'if (global.isFile("D:\temp\devCliTest\1001\showTestRunner.js")) return "File found"; else return "File not found";}';

When I debug in win7, D7 or D2010, this is the value that I get for fName:

'D:'#9'empdevCliTest@1showTestRunner.js'

Strange string indeed! So obviouly, the file is not found!

My question is : What is the string format to be expected on Delphi side ?

I'm sure, it is a very simple question for once! wink

Thanks!

jf

Offline

#2 2015-08-11 21:02:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Javascript strings args format in delphi callbacks

AFAIR \ should be escaped in a js string, using \\

Offline

#3 2015-08-11 21:22:53

jf
Member
Registered: 2015-08-11
Posts: 2

Re: Javascript strings args format in delphi callbacks

I was sure I tried it! Sorry...

Tx!

jf

Offline

Board footer

Powered by FluxBB