2

I was looking for a way to log down messages the installer log using JScript and stumbled upon this answer:

How to debug an MSI Custom Action that is implemented in Javascript?

It works great. However there is something intriguing in the code that I haven't been able to figure out. In this method:

// spool an informational message into the MSI log, if it is enabled. 
function LogMessage(msg) {
    var record = Session.Installer.CreateRecord(0);
    record.StringData(0) = "CustomAction:: " + msg;
    Session.Message(MsgKind.Log, record);
}

How does the line record.StringData(0) = "CustomAction:: " + msg; work, from a syntax/semantic perspective? It looks to me this is trying to assign a value to the return value of a function call, which should be illegal or a no-op at best? Yet it works, and the message is printed out in the log.

What am I missing?

Chin
  • 19,717
  • 37
  • 107
  • 164
  • @JavaScript That isn't true for JavaScript, where the above would fail anyway; in JScript, however, Microsoft has created some 'special behaviour', that allows this on some special objects. – FZs Aug 05 '20 at 17:16

1 Answers1

1

It is a JScript way to access lists and not an actual function. Therefore it does not throw an Invalid left-hand side in assignment.

StringData is a list of values. What you are actually setting is the value of the 0 index. It is like setting the value to an array using arr[0] = 'xyz'. In your example you could also omit it:

record.StringData = "CustomAction:: " + msg;

Syntax

propVal = Record.StringData
Record.StringData = propVal 

Property value

Required field number of the value within the record, 1-based.

Remarks

The returned value of a nonexistent field is an empty string. To set a record string field to null, use either an empty variant or an empty string. Attempting to store a value in a nonexistent field causes an error.

Source: Patrick

Lain
  • 3,657
  • 1
  • 20
  • 27
  • It's strange, apart from that answer from Patrick I haven't seen any mention of `()` being used as the index operator anywhere. Seems like a very obscure feature/syntax – Chin Aug 05 '20 at 20:26
  • @Chin: It is the only reference I found myself as well. I remember it pretty common in *ASP*, *VB* and *VB.Net*. There is usually is a *shorthand* for `x.item()`, yet I could not find any actual origin or implementation reference. – Lain Aug 05 '20 at 22:03