This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
forked from cappuccino/cappuccino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileExecutable.js
136 lines (104 loc) · 3.76 KB
/
FileExecutable.js
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* FileExecutable.js
* Objective-J
*
* Created by Francisco Tolmasky.
* Copyright 2010, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
var FileExecutablesForURLStrings = { };
function FileExecutable(/*CFURL|String*/ aURL)
{
aURL = makeAbsoluteURL(aURL);
var URLString = aURL.absoluteString(),
existingFileExecutable = FileExecutablesForURLStrings[URLString];
if (existingFileExecutable)
return existingFileExecutable;
FileExecutablesForURLStrings[URLString] = this;
var fileContents = StaticResource.resourceAtURL(aURL).contents(),
executable = NULL,
extension = aURL.pathExtension();
if (fileContents.match(/^@STATIC;/))
executable = decompile(fileContents, aURL);
else if (extension === "j" || !extension)
executable = exports.preprocess(fileContents, aURL, Preprocessor.Flags.IncludeDebugSymbols);
else
executable = new Executable(fileContents, [], aURL);
Executable.apply(this, [executable.code(), executable.fileDependencies(), aURL, executable._function]);
this._hasExecuted = NO;
}
exports.FileExecutable = FileExecutable;
FileExecutable.prototype = new Executable();
#ifdef COMMONJS
FileExecutable.allFileExecutables = function()
{
var URLString,
fileExecutables = [];
for (URLString in FileExecutablesForURLStrings)
if (hasOwnProperty.call(FileExecutablesForURLStrings, URLString))
fileExecutables.push(FileExecutablesForURLStrings[URLString]);
return fileExecutables;
}
#endif
FileExecutable.prototype.execute = function(/*BOOL*/ shouldForce)
{
if (this._hasExecuted && !shouldForce)
return;
this._hasExecuted = YES;
Executable.prototype.execute.call(this);
}
DISPLAY_NAME(FileExecutable.prototype.execute);
FileExecutable.prototype.hasExecuted = function()
{
return this._hasExecuted;
}
DISPLAY_NAME(FileExecutable.prototype.hasExecuted);
function decompile(/*String*/ aString, /*CFURL*/ aURL)
{
var stream = new MarkedStream(aString);
/*
if (stream.version !== "1.0")
return;
*/
var marker = NULL,
code = "",
dependencies = [];
while (marker = stream.getMarker())
{
var text = stream.getString();
if (marker === MARKER_TEXT)
code += text;
else if (marker === MARKER_IMPORT_STD)
dependencies.push(new FileDependency(new CFURL(text), NO));
else if (marker === MARKER_IMPORT_LOCAL)
dependencies.push(new FileDependency(new CFURL(text), YES));
}
var fn = FileExecutable._lookupCachedFunction(aURL)
if (fn)
return new Executable(code, dependencies, aURL, fn);
return new Executable(code, dependencies, aURL);
}
var FunctionCache = { };
FileExecutable._cacheFunction = function(/*CFURL|String*/ aURL, /*Function*/ fn)
{
aURL = typeof aURL === "string" ? aURL : aURL.absoluteString();
FunctionCache[aURL] = fn;
}
FileExecutable._lookupCachedFunction = function(/*CFURL|String*/ aURL)
{
aURL = typeof aURL === "string" ? aURL : aURL.absoluteString();
return FunctionCache[aURL];
}