3

When I use the JSC (JavaScriptCore) engine provided in the System Library, it acts differently then when using Safari's debug console

$ /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
>>> console.log("hello");
Exception: TypeError: undefined is not an object (evaluating 'console.log')

When console.log("hello"); works perfectly fine in Safari.

sdc
  • 2,603
  • 1
  • 27
  • 40
  • 1
    Because there's no `console` object in JSC, apparently. – Barmar Jan 21 '16 at 02:37
  • @Barmar can you convert this to an answer so I can accept it? - Thanks – sdc Jan 21 '16 at 02:45
  • I don't know anything about JSC, I just based that on the symptom. What's wrong with John Hascall's answer, that explains how to solve it? – Barmar Jan 21 '16 at 02:47
  • @Barmar the problem has nothing to do with an Objective C or C environment but a JSC environment – sdc Jan 21 '16 at 03:04
  • OK, I've reopened the question. You should put your solution in an answer, not the question. – Barmar Jan 21 '16 at 03:20

4 Answers4

7

TL;DR

var Console = function () {
    this.log = function(msg){ debug(msg) }; 
};
var console = new Console();
console.log("hello");

Safari creates a console object that is available in the debug console, but not in the JSC environment. See Safari's console documentation here

Adding my own console object that wraps the JSC debug method solved my problem:

$ /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
>>> var Console = function () {
...     this.log = function(msg){ debug(msg) };
... };
undefined
>>> var console = new Console();
undefined
>>> console.log("hello");
-> hello
undefined
sdc
  • 2,603
  • 1
  • 27
  • 40
  • Does that mean that the debug() does everything that console.log normally does ? That's awesome! Please tell me it is what I think it is!!! – Rahul Iyer Oct 28 '19 at 05:43
  • @KaizerSozay unfortunately, it is not. This is just a hacky way of allowing calls to `console.log`. For it to function like the actual console object you would need to implement everything Webkit ([source](https://github.com/WebKit/webkit/blob/89c28d471fae35f1788a0f857067896a10af8974/Source/JavaScriptCore/runtime/ConsoleObject.cpp)), or V8 ([source](https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/d8-console.cc)) implements depending on the console object flavor you want. @Sunil is going to be a little more robust, as it handles multiple params – sdc Oct 31 '19 at 03:48
2

The console object doesn't exist in JSC -- you can add it if you like JavaScriptCore console.log

Community
  • 1
  • 1
John Hascall
  • 9,176
  • 6
  • 48
  • 72
2

I've ended up with this one liner that works on other JS engines along with JSC compatibility:

console = console || { log: (...args) => debug(Array.prototype.slice.call(args).join(' ')) }
Sunil
  • 728
  • 5
  • 7
1

I learned from the answers above.

Though, as I not see a goal, I presume it was to see output on stdout:

print('string')

And when ready, use a stream editor to substitute 'print' with 'console.log'.

ziff
  • 339
  • 1
  • 3
  • 16