File size: 1,838 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
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * SimplePlugin.js: Example for an external Squeak VM module
 *
 * primNavigatorInfo: anInteger
 *     <primitive: 'primitiveNavigatorInfo' module: 'SimplePlugin'>
 *     ^ self primitiveFailed
 */
 
function SimplePlugin() {
    var interpreterProxy,
        primHandler;

    function setInterpreter(anInterpreterProxy) {
        // Slang interface
        interpreterProxy = anInterpreterProxy;
        // PrimHandler methods for convenience
        primHandler = interpreterProxy.vm.primHandler;
        // success
        return true;
    };

    function primitiveNavigatorInfo(argCount) {
        if (argCount !== 1) return false; // fail
        var which = interpreterProxy.stackIntegerValue(0);
        if (interpreterProxy.failed()) return false; // fail
        var result = getNavigatorInfo(which);
        if (!result) return false; // fail
        var resultObj = primHandler.makeStString(result);
        interpreterProxy.popthenPush(1 + argCount, resultObj);
        return true; // success
    };

    function getNavigatorInfo(index) {
        switch (index) {
            case 1: return navigator.userAgent;
            case 2: return navigator.language;
        }
    };

    // hide private functions
    return {
        setInterpreter: setInterpreter,
        primitiveNavigatorInfo: primitiveNavigatorInfo,
    }
};

// register plugin in global Squeak object
window.addEventListener("load", function() {
    Squeak.registerExternalModule('SimplePlugin', SimplePlugin());
});

/**********************************
NOTE: the mini.image does not have compiler support for
named primitives, yet. You need to declare it manually 
using prim 117:

primNavigatorInfo: anInteger
    <primitive: 117>
    #(SimplePlugin primitiveNavigatorInfo 0 0) at: 1.
    ^ self primitiveFailed
***********************************/