File size: 1,297 Bytes
8f3f8db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * This plugin simply adds "console.log" functionality.
 * 
 * Add the following method to the Smalltalk image (to Object for example) to use it:
 * primLog: messageString level: levelString
 *
 *	"Log messageString to the console. The specified level should be one of:
 *		'log'
 *		'info'
 *		'warn'
 *		'error'
 *	"
 *
 * 	<primitive: 'primitiveLog:level:' module: 'ConsolePlugin'>
 *	^ self
 */

function ConsolePlugin() {
  "use strict";

  return {
    getModuleName: function() { return "ConsolePlugin"; },
    interpreterProxy: null,

    setInterpreter: function(anInterpreter) {
      this.interpreterProxy = anInterpreter;
      return true;
    },

    // Logging
    "primitiveLog:level:": function(argCount) {
      if (argCount !== 2) return false;
      var message = this.interpreterProxy.stackValue(1).bytesAsString();
      var level = this.interpreterProxy.stackValue(0).bytesAsString();
      console[level](message);
      this.interpreterProxy.pop(argCount);	// Answer self
      return true;
    }
  };
}

function registerConsolePlugin() {
    if (typeof Squeak === "object" && Squeak.registerExternalModule) {
        Squeak.registerExternalModule("ConsolePlugin", ConsolePlugin());
    } else self.setTimeout(registerConsolePlugin, 100);
};

registerConsolePlugin();