Skip to content

Execute JS commands by printing directly into the file stream instead of using BuildCommand #29

@Natrium729

Description

@Natrium729

Here's a suggestion to improve the usability of executing JavaScript commands.

Instead of building a command by concatenating strings/arrays with BuildCommand:

VorpleExecuteJavaScriptCommand(BuildCommand("alert('Score: ", score, "')"));

we would open the stream of the JS eval file and print the command directly into it.

VorpleJavaScriptCommand(); ! Opens the file stream.
print "alert('Score: ", score, "')";
VorpleSendJavaScriptCommand(); ! Closes the file stream and sends the command to Vorple.

There'd still be a way to execute a single string, by giving an argument to VorpleJavaScriptCommand (or by using another function):

VorpleJavaScriptCommand("alert('Hello World')"); ! Executes a single string.

Pros:

  • We are not restricted by the 7-argument limit of BuildCommand.
  • We can use printing functions, such as print (char) or print (address).
  • We can write arbitrary code after opening the stream, so it's possible to use conditions or loops, like in I7 texts. That means it's easier to build complex JavaScript commands.
  • There's no need to handle edge cases like in VorpleTooltipElementDuration (where we have to open manually the stream because there are two IntToString working on the same array), since it becomes the standard way to directly print into the stream.
  • There's no more the ugly BuildCommand.
  • I think we wouldn't need the equally ugly hugehugestr array anymore since we print directly into the file stream, resulting into a smaller and faster story file.
  • It's in my opinion more idiomatic in Inform 6 to work on streams instead of working on strings/arrays.

Cons:

  • It takes three lines to execute a simple command like above, instead of only one. This should be solvable by making VorpleJavaScriptCommand (or another routine) take arguments and making it print them into the stream: VorpleJavaScriptCommand("alert('Score: ", score, "')").
  • It's a breaking change, except if we keep the old functions of course. (But if we keep them, the pro of having a smaller and faster story file doesn't apply anymore.)
  • It will lead to strange bugs if the author forgets to close the stream with VorpleSendJavaScriptCommand. (But that shouldn't happen often since it's easy to notice.)
  • Authors will have to be careful to handle Unicode in strings and escaping. (But that's easily done with print (VorpleEscape) uni-string.
  • We should make sure that everything plays well with other functions that open streams (e.g. print (A) obj opens a memory stream to determine whether the object starts with a vowel).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions