~ubuntu-branches/ubuntu/trusty/almond/trusty

« back to all changes in this revision

Viewing changes to tests/circular/transpiler/transpiler.js

  • Committer: Package Import Robot
  • Author(s): Georges Khaznadar
  • Date: 2013-11-02 16:28:01 UTC
  • Revision ID: package-import@ubuntu.com-20131102162801-c33r5wt6efwq52n7
Tags: upstream-0.2.6
ImportĀ upstreamĀ versionĀ 0.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
/*jslint strict: false, plusplus: false */
 
4
/*global define: false, require: false,  XMLHttpRequest: false, ActiveXObject: false,
 
5
  window: false, Packages: false, java: false, process: false */
 
6
 
 
7
(function () {
 
8
    //Load the text plugin, so that the XHR calls can be made.
 
9
    var buildMap = {}, fetchText, fs,
 
10
        progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
 
11
 
 
12
    function createXhr() {
 
13
        //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
 
14
        var xhr, i, progId;
 
15
        if (typeof XMLHttpRequest !== "undefined") {
 
16
            return new XMLHttpRequest();
 
17
        } else {
 
18
            for (i = 0; i < 3; i++) {
 
19
                progId = progIds[i];
 
20
                try {
 
21
                    xhr = new ActiveXObject(progId);
 
22
                } catch (e) {}
 
23
 
 
24
                if (xhr) {
 
25
                    progIds = [progId];  // so faster next time
 
26
                    break;
 
27
                }
 
28
            }
 
29
        }
 
30
 
 
31
        if (!xhr) {
 
32
            throw new Error("require.getXhr(): XMLHttpRequest not available");
 
33
        }
 
34
 
 
35
        return xhr;
 
36
    }
 
37
 
 
38
    if (typeof window !== "undefined" && window.navigator && window.document) {
 
39
        fetchText = function (url, callback) {
 
40
            var xhr = createXhr();
 
41
            xhr.open('GET', url, true);
 
42
            xhr.onreadystatechange = function (evt) {
 
43
                //Do not explicitly handle errors, those should be
 
44
                //visible via console output in the browser.
 
45
                if (xhr.readyState === 4) {
 
46
                    callback(xhr.responseText);
 
47
                }
 
48
            };
 
49
            xhr.send(null);
 
50
        };
 
51
    } else if (typeof process !== "undefined" &&
 
52
             process.versions &&
 
53
             !!process.versions.node) {
 
54
        //Using special require.nodeRequire, something added by r.js.
 
55
        fs = require.nodeRequire('fs');
 
56
 
 
57
        fetchText = function (url, callback) {
 
58
            callback(fs.readFileSync(url, 'utf8'));
 
59
        };
 
60
    } else if (typeof Packages !== 'undefined') {
 
61
        //Why Java, why is this so awkward?
 
62
        fetchText = function (url, callback) {
 
63
            var encoding = "utf-8",
 
64
                file = new java.io.File(url),
 
65
                lineSeparator = java.lang.System.getProperty("line.separator"),
 
66
                input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
 
67
                stringBuffer, line,
 
68
                content = '';
 
69
            try {
 
70
                stringBuffer = new java.lang.StringBuffer();
 
71
                line = input.readLine();
 
72
 
 
73
                // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
 
74
                // http://www.unicode.org/faq/utf_bom.html
 
75
 
 
76
                // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
 
77
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
 
78
                if (line && line.length() && line.charAt(0) === 0xfeff) {
 
79
                    // Eat the BOM, since we've already found the encoding on this file,
 
80
                    // and we plan to concatenating this buffer with others; the BOM should
 
81
                    // only appear at the top of a file.
 
82
                    line = line.substring(1);
 
83
                }
 
84
 
 
85
                stringBuffer.append(line);
 
86
 
 
87
                while ((line = input.readLine()) !== null) {
 
88
                    stringBuffer.append(lineSeparator);
 
89
                    stringBuffer.append(line);
 
90
                }
 
91
                //Make sure we return a JavaScript string and not a Java string.
 
92
                content = String(stringBuffer.toString()); //String
 
93
            } finally {
 
94
                input.close();
 
95
            }
 
96
            callback(content);
 
97
        };
 
98
    }
 
99
 
 
100
    define('refine',[],function () {
 
101
        return {
 
102
            load: function (name, parentRequire, load, config) {
 
103
                var url = parentRequire.toUrl(name + '.refine');
 
104
                fetchText(url, function (text) {
 
105
                    text = text.replace(/refine\s*\(/g, 'define(');
 
106
 
 
107
                    if (config.isBuild) {
 
108
                        buildMap[name] = text;
 
109
                    }
 
110
 
 
111
                    //Add in helpful debug line
 
112
                    text += "\r\n//@ sourceURL=" + url;
 
113
 
 
114
                    load.fromText(text);
 
115
 
 
116
                    parentRequire([name], function (value) {
 
117
                        load(value);
 
118
                    });
 
119
                });
 
120
            },
 
121
 
 
122
            write: function (pluginName, name, write) {
 
123
                if (name in buildMap) {
 
124
                    var text = buildMap[name];
 
125
                    write.asModule(pluginName + "!" + name, text);
 
126
                }
 
127
            }
 
128
        };
 
129
    });
 
130
 
 
131
}());
 
132
 
 
133
define('refine!c',['refine!a', 'exports'], function (a, exports) {
 
134
    exports.name = 'c';
 
135
    exports.a = a;
 
136
});
 
137
 
 
138
define('refine!b',['refine!c', 'exports'], function (c, exports) {
 
139
    exports.name = 'b';
 
140
    exports.c = c;
 
141
});
 
142
 
 
143
define('refine!a',['refine!b', 'exports'], function (b, exports) {
 
144
    exports.name = 'a';
 
145
    exports.b = b;
 
146
});
 
147
 
 
148
define('refine!e',['refine!d'], function(d) {
 
149
    function e() {
 
150
        return e.name + require('refine!d').name;
 
151
    }
 
152
 
 
153
    e.name = 'e';
 
154
 
 
155
    return e;
 
156
});
 
157
define('refine!d',['refine!e'], function(e) {
 
158
    function d() {
 
159
        return require('refine!e')();
 
160
    }
 
161
 
 
162
    d.name = 'd';
 
163
 
 
164
    return d;
 
165
});
 
166
require({
 
167
        baseUrl: requirejs.isBrowser ? './' : './circular/transpiler',
 
168
        paths: {
 
169
            'text': '../../../../text/text',
 
170
            'refine': '../../plugins/fromText/refine'
 
171
        }
 
172
    },
 
173
    ["require", "refine!a", "refine!b", "refine!d"],
 
174
    function(require, a, b, d) {
 
175
        doh.register(
 
176
            "circularTranspiler",
 
177
            [
 
178
                function circularTranspiler(t) {
 
179
                    t.is("a", a.name);
 
180
                    t.is("b", a.b.name);
 
181
                    t.is("c", a.b.c.name);
 
182
                    t.is("b", b.name);
 
183
                    t.is("c", b.c.name);
 
184
                    t.is("ed", d());
 
185
                 }
 
186
            ]
 
187
        );
 
188
        doh.run();
 
189
    }
 
190
);
 
191
 
 
192
define("transpiler-tests", function(){});