-1

I'm trying to determine a function's result depending on the caller's ScriptEngine:

//JScript
function doSomething()
{
    if (ScriptEngine() === "VBScript")
        return "this is VBScript";
    else
        return "this is JScript";
}

But the results are:

//JScript
var str = doSomething(); //"this is JScript"

//VBScript
Dim str : str = DoSomething() '"this is JScript"

The ScriptEngine() function always returns its caller's engine name, but I would like to know the doSomething() caller's engine name (without passing it as a parameter). Is there any way to do this in ASP?

The motivation behind this is that I wrote a JSON utility class to use in an ASP site. The parsing of the JSON string is done via eval (using the validation that I found in this answer).

The result of eval is a JScript object (with JScript arrays and JScript dates as properties). Then the user must specify a flag to tell the function if he wants the pure JScript object or if he wants a VBScript object (with arrays and dates converted to their VBScript equivalent). I would like to eliminate that parameter.

//JScript
function parseJson(jsonString, isJsObject)
{
    var parsedObject = parsingLogic(jsonString);

    if (!isJsObject) //Would like to replace by engine check
        convertVbProperties(parsedObject);

    return parsedObject;
}
Community
  • 1
  • 1
Raphael
  • 1,847
  • 1
  • 17
  • 29
  • You may get better help if it was clear why this is important. Why does it matter which language the code calling the function is written in in the context of your problem? – AnonJr Aug 28 '13 at 16:47
  • @AnonJr, I have added the motivation for the question. The `isJsObject` parameter is no great issue, but it would be nice to get rid of it. – Raphael Aug 28 '13 at 17:13
  • That makes sense. I don't have an answer just yet, but this will help others who might. – AnonJr Aug 28 '13 at 17:54
  • i don't think that's possible but you could always return both objects so the caller can decide which one it wants to use... – ulluoink Aug 29 '13 at 07:08
  • @ulluoink Nah, I think the flagged version is still better... *sigh* – Raphael Aug 29 '13 at 12:53
  • 1
    The [`caller`](http://msdn.microsoft.com/en-us/library/7t96kt3h%28v=vs.84%29.aspx) property is _so_ close to being an answer. It always returns `null` when called from VBScript (at least in WSH, not documented behavior), _but_, it can also return `null` when called from top-level JScript (that _is_ documented behavior), so it's not really reliable. – Cheran Shunmugavel Aug 13 '14 at 07:04

1 Answers1

-1

I ended up keeping the isJsObject parameter. Since we were using VBScript most of the time, and JScript was only being used to build libraries, that worked fine.

Raphael
  • 1,847
  • 1
  • 17
  • 29