extism / js-pdk

Write Extism plugins in JavaScript & TypeScript
42 stars 16 forks source link

Update console.log implementation to support multiple params #80

Closed ubiquitous-dev closed 4 weeks ago

ubiquitous-dev commented 1 month ago

Issue Summary

This PR fixes a couple of incompatibilities between the console.debug/log/warn/error statements. console.log("a", "b", "c"); in the exits js-pdk does not output the expected a b c but just outputs a. also, an empty console.log errors out, but it normally is a no-op.

Test script

  console.debug();
  console.debug("debug: single console statement");
  console.debug("debug:", "multiple", "console", "statements");

  console.log();
  console.log("log: single console statement");
  console.log("log:", "multiple", "console", "statements");

  console.warn();
  console.warn("warn: single console statement");
  console.warn("warn:", "multiple", "console", "statements");

  console.error();
  console.error("error: single console statement");
  console.error("error:", "multiple", "console", "statements");

Output in chrome devtools

image

Output in current js-pdk

2024/05/26 11:18:19 Calling function : greet
Error: Uncaught InternalError: Need at least one arg
    at greet (script.js:43)

note that the no-op behavior of an empty call is not present here and it instead errors.

Output in current js-pdk with empty calls removed

2024/05/26 11:19:26 debug: single console statement
2024/05/26 11:19:26 debug:
2024/05/26 11:19:26 log: single console statement
2024/05/26 11:19:26 log:
2024/05/26 11:19:26 warn: single console statement
2024/05/26 11:19:26 warn:
2024/05/26 11:19:26 error: single console statement
2024/05/26 11:19:26 error:

note the multi-statement lines do not print all arguments.

Output with the code change from the PR (including empty statements)

024/05/26 11:23:07 Calling function : greet
2024/05/26 11:23:08 debug: single console statement
2024/05/26 11:23:08 debug: multiple console statements
2024/05/26 11:23:08 log: single console statement
2024/05/26 11:23:08 log: multiple console statements
2024/05/26 11:23:08 warn: single console statement
2024/05/26 11:23:08 warn: multiple console statements
2024/05/26 11:23:08 error: single console statement
2024/05/26 11:23:08 error: multiple console statements

note that the empty statements are properly no-ops, and the multiple args are properly printed.

bhelx commented 1 month ago

Closes https://github.com/extism/js-pdk/issues/70

ubiquitous-dev commented 4 weeks ago

@bhelx anything else you'd like to see to make this a candidate for merging? I keep forgetting that console.log doesn't take multiple parameters in the pdk

bhelx commented 4 weeks ago

Apologies. I thought I merged it already!