/ Check-in [bf58fc7954]
DEMO | DOWNLOAD | DEPLOY | SEARCH
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:jsig updates
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:bf58fc7954da18ec5b9ea02997e43bfdd1dbb12e
User & Date: pmacdona 2019-06-25 15:05:56
Context
2019-06-25
15:10
Doc check-in: 01d0606168 user: pmacdona tags: trunk
15:05
jsig updates check-in: bf58fc7954 user: pmacdona tags: trunk
15:01
Add RegExp constructor as well. check-in: 323ea31810 user: pmacdona tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to js-demos/jsig.html.

1
2
3
4
5
6
7
8
9
10
Typed Function Arguments demo.
<p>
See warnings in console <i>(F12, Ctrl-Shift-I or Inspect-Element)</i>

<script src="../lib/web/jsi.js"></script>
<script>
    $jsi.include('bark.jsi');
    $jsi.onload(function () {
        bark('abc',0);
        bark(9,0);


|







1
2
3
4
5
6
7
8
9
10
Typed Function Arguments demo.
<p>
To see warnings open console <i>(F12, Ctrl-Shift-I or Inspect-Element)</i> and reload page.

<script src="../lib/web/jsi.js"></script>
<script>
    $jsi.include('bark.jsi');
    $jsi.onload(function () {
        bark('abc',0);
        bark(9,0);

Changes to lib/web/jsi.js.

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
286
287
288
289
290
291
292

293
294
295
296
297
298
299
...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

428
429



430
431
432
433
434
435
436
...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
        uuid:0,
        srcList:[],
        srcCnt:0,
        errCnt:0,
        errCur:null,
        funcLst:[]
    },
    setOpts: function(obj, opts) { // Set options in object.
        for (var i in obj)
            if (opts[i] === undefined)
                throw("unknown option: "+i+' is not one of: '+Object.keys(opts).join(', '));
        for (var i in opts)
            if (obj[i] === undefined)
                obj[i] = opts[i];
        return obj;
................................................................................
                errorCmd(msg);
            jsi._state.errCur = msg;
        }
        return msg;
    },

    filesave: function(filename, data, mime) { // Save data as filename.

        var blob = new Blob([data], {type: (mime?mime:'text/html')});
        if(window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var elem = window.document.createElement('a');
            elem.href = window.URL.createObjectURL(blob);
            elem.download = filename;
................................................................................
    },

    guid: function() { // Return unique UUID
        if (!jsi._state.uuid)
            jsi._state.uuid = Date.now();
        return '_uuid'+(jsi._state.uuid++).toString(16);
    },
    ajax:function(url, opts) { // Ajax
        function none(){};
        if (typeof url !== 'string') {
            opts = url;
            url = opts.url;
        }
        if (!url) return false;
    
        opts = jsi.setOpts(opts, {
            success:    none,
            error:      none,
            complete:   none,
            type:       'GET',
            dataType:   'text', // One of: json, jsonp, script, text.
            data:       {},   // Query data.
            headers:    {},
            async:      true,
            url:        null
        });

    
        var req, qd = null, dt = opts.dataType;



        switch (dt) {
        case 'jsonp': case 'script': {
            req = document.createElement('script');    
    
            window[opts.data.callback = jsi.guid()] = function(data) {
                opts.success.call(req, data, null, req);
            };
................................................................................
                req.remove();
            };
    
            qd = '?';
            for (var key in opts.data)
                qd += encodeURIComponent(key) + '=' + encodeURIComponent(opts.data[key]) +'&';
    
            req.src = url + qd;
            document.head.appendChild(req);
            break;
        }
        case 'text':
        case 'json': {
            req = new XMLHttpRequest();
            req.open(opts.type, url, opts.async);
            opts.headers["X-Requested-With"] = "XMLHttpRequest";
            for (var key in opts.headers)
                req.setRequestHeader(key, opts.headers[key]);
    
            req.onload = req.onerror = function onload() {
                var text = req.statusText;
                if ( req.status < 200 || req.status >= 400 )
................................................................................
            fns = [fns];
        for (var i in fns) {
            var fn = fns[i];
            if (window.jsiWebSocket)
                return addScript(fn);
            jsi._state.srcList.push(fn);
            jsi._state.srcCnt++;
            jsi.ajax(fn, {
                success: function(str, txt, req) {
                    jsi._state.srcCnt--;
                    LogDebug('Src Success: '+str);
                    var hdrs = req.getAllResponseHeaders();
                    if (!hdrs || hdrs.indexOf('jsiWebSocket')<0) {
                        str = SigConvert(str);
                        LogDebug('Src xlate: '+str);







|







 







>







 







|

<
<
<
|
<
<
|
|
|
|







>


>
>
>







 







|






|







 







|







192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
...
403
404
405
406
407
408
409
410
411



412


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
        uuid:0,
        srcList:[],
        srcCnt:0,
        errCnt:0,
        errCur:null,
        funcLst:[]
    },
    setopts: function(obj, opts) { // Set options in object.
        for (var i in obj)
            if (opts[i] === undefined)
                throw("unknown option: "+i+' is not one of: '+Object.keys(opts).join(', '));
        for (var i in opts)
            if (obj[i] === undefined)
                obj[i] = opts[i];
        return obj;
................................................................................
                errorCmd(msg);
            jsi._state.errCur = msg;
        }
        return msg;
    },

    filesave: function(filename, data, mime) { // Save data as filename.

        var blob = new Blob([data], {type: (mime?mime:'text/html')});
        if(window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var elem = window.document.createElement('a');
            elem.href = window.URL.createObjectURL(blob);
            elem.download = filename;
................................................................................
    },

    guid: function() { // Return unique UUID
        if (!jsi._state.uuid)
            jsi._state.uuid = Date.now();
        return '_uuid'+(jsi._state.uuid++).toString(16);
    },
    ajax:function(opts) { // Ajax
        function none(){};



            


        opts = jsi.setopts(opts, {
            success:    null,
            error:      null,
            complete:   null,
            type:       'GET',
            dataType:   'text', // One of: json, jsonp, script, text.
            data:       {},   // Query data.
            headers:    {},
            async:      true,
            url:        null
        });
        if (!opts.url) throw('url is required');
    
        var req, qd = null, dt = opts.dataType;
        if (!opts.success) opts.success = none;
        if (!opts.error) opts.error = none;
        if (!opts.complete) opts.complete = none;
        switch (dt) {
        case 'jsonp': case 'script': {
            req = document.createElement('script');    
    
            window[opts.data.callback = jsi.guid()] = function(data) {
                opts.success.call(req, data, null, req);
            };
................................................................................
                req.remove();
            };
    
            qd = '?';
            for (var key in opts.data)
                qd += encodeURIComponent(key) + '=' + encodeURIComponent(opts.data[key]) +'&';
    
            req.src = opts.url + qd;
            document.head.appendChild(req);
            break;
        }
        case 'text':
        case 'json': {
            req = new XMLHttpRequest();
            req.open(opts.type, opts.url, opts.async);
            opts.headers["X-Requested-With"] = "XMLHttpRequest";
            for (var key in opts.headers)
                req.setRequestHeader(key, opts.headers[key]);
    
            req.onload = req.onerror = function onload() {
                var text = req.statusText;
                if ( req.status < 200 || req.status >= 400 )
................................................................................
            fns = [fns];
        for (var i in fns) {
            var fn = fns[i];
            if (window.jsiWebSocket)
                return addScript(fn);
            jsi._state.srcList.push(fn);
            jsi._state.srcCnt++;
            jsi.ajax({url:fn,
                success: function(str, txt, req) {
                    jsi._state.srcCnt--;
                    LogDebug('Src Success: '+str);
                    var hdrs = req.getAllResponseHeaders();
                    if (!hdrs || hdrs.indexOf('jsiWebSocket')<0) {
                        str = SigConvert(str);
                        LogDebug('Src xlate: '+str);

Changes to md/jsi.js.md.

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
..
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





63
64
65
66
67


68
69
















70





jsijs
====

<div id="sectmenu"></div>
[jsi.js](/file/lib/web/jsi.js)
contains browser side support for the following features:


Typed Functions

----
**jsi.js** supports [typed function](Types.md) like **bark.jsi**:














    function bark(s:string, n:number) {
       puts("BARK: "+s);
    }

used like so:

    <script src="/jsi/web/jsi.js"></script>
    <script>
        $jsi.include('bark.jsi');
        $jsi.onload(function() {
            bark();
            bark('abc',0);
            bark(9,0);
            bark('9','0');
            bark(9);
            bark(9,0,0);
        });
        
................................................................................
    BARK: 9
    jsi.js:23 type mismatch for arg 2 "n" expected "number" got "string" 0: calling bark(s:string, n:number)
    BARK: 9
    jsi.js:23 missing arguments: expected 2 got 1: calling bark(s:string, n:number)
    BARK: 9
    jsi.js:23 extra arguments: expected 2 got 3: calling bark(s:string, n:number)
    BARK: 9
    
Click [here](../js-demos/jsig.html).

Manual
----


    <script src="/jsi/web/jsi.js"></script>
    <script>
    function bark(s, n) { $jsig('bark(s:string, n:number)');
       puts("BARK: "+s);
    }
    </script>

Jsish Server

----

When using **jsish** web server, **.jsi** is handled automatically.
Here is an example:






    <script src="/web/jsi.js"></script>
    <script src="big.js"></script>





    
with **big.jsi**:

    function big(s:string, n:number) {
       console.log("BIG: "+s+n);


    }    
    big(1,2);






















|

>


<
>

<
>

<
>

>
>
>
>
>
>
>
>
>
>
>
>

|








<







 







|
|

|
<
>


|
<
<
|
<

<
>


<
<
>

>
>
>
>
|
|
>
>
>
>
>
|
<

<
<
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
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
..
47
48
49
50
51
52
53
54
55
56
57

58
59
60
61


62

63

64
65
66


67
68
69
70
71
72
73
74
75
76
77
78
79
80

81


82
83
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
jsi.js
====

<div id="sectmenu"></div>
[jsi.js](/file/lib/web/jsi.js)

provides browser side features,


$jsig: ƒ (sig, args)
----

Function argument [type-checking](Types.md) is provided by **$jsig**:

    <script src="/jsi/web/jsi.js"></script>
    <script>
    function bark(s, n) { $jsig('bark(s:string, n:number)', arguments);
       console.log("BARK: "+s);
    }
    </script>

### Pre-process

More convenient is to use preprocessed **.jsi** files, as seen in action [here](../js-demos/jsig.html):

    // FILE: bark.jsi
    function bark(s:string, n:number) {
       console.log("BARK: "+s);
    }

used like so:

    <script src="/jsi/web/jsi.js"></script>
    <script>
        $jsi.include('bark.jsi');
        $jsi.onload(function() {

            bark('abc',0);
            bark(9,0);
            bark('9','0');
            bark(9);
            bark(9,0,0);
        });
        
................................................................................
    BARK: 9
    jsi.js:23 type mismatch for arg 2 "n" expected "number" got "string" 0: calling bark(s:string, n:number)
    BARK: 9
    jsi.js:23 missing arguments: expected 2 got 1: calling bark(s:string, n:number)
    BARK: 9
    jsi.js:23 extra arguments: expected 2 got 3: calling bark(s:string, n:number)
    BARK: 9

### Server-side

When using **jsish** as the web server, **.jsi** files will already be preprocessed,

making **include** unnecessary, eg.

    <script src="/jsi/web/jsi.js"></script>
    <script src="bark.js"></script>






ajax: ƒ (opts)
----



Compatiable with a subset of JQuery **ajax**.  Supported opts are:

Option    | Default | Description
----------|---------|-----
success   | none    | Called on success
error     | none    | Called on error
complete  | none    | Called when complete
type      | 'GET'   | |
dataType  | 'text'  | One of json, jsonp, script, text.
data      | {}      | Query data.
headers   | {}      | Headers to pass
async     | true    | Is asyncronous
url       | null    | Url of resources





conf: ƒ (vals)
----


filesave: ƒ (filename, data, mime)
----
guid: ƒ ()
----
htmladd: ƒ (str)
----
include: ƒ (fns)
----
onload: ƒ (f)
----
schema: ƒ (obj, schm)
----
setopts: ƒ (obj, opts)
----
websock: ƒ (prot)
----

Globals
====
$jsi
----
$