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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# SPDX-License-Identifier: CC0-1.0
#
# Process javascript files and resolve dependencies between them
#
# This file is part of Haketilo
#
# Copyright (C) 2021, Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program 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
# CC0 1.0 Universal License for more details.
function read_file(filename,
imports_state, exports_state, line, record, result) {
imports_state = "not_started"
exports_state = "not_started"
do {
result = (getline line < filename)
if (result < 0) {
printf "error reading %s", filename
exit 1
}
if (imports_state == "started" &&
line ~ /^([[:space:]]*\*[[:space:]]+)?IMPORT[[:space:]]+[_a-zA-Z][_a-zA-Z0-9]*[[:space:]]*$/) {
record = line
sub(/^([[:space:]]*\*[[:space:]]+)?IMPORT[[:space:]]+/, "", record)
sub(/([[:space:]]+$)/, "", record)
imports[filename,++import_counts[filename]] = record
}
if (imports_state == "started" &&
line ~ /^([[:space:]]*\*[[:space:]]+)?IMPORTS_END[[:space:]]*$/)
imports_state = "finished"
if (imports_state == "not_started" &&
line ~ /^([[:space:]]*\*[[:space:]]+)?IMPORTS_START[[:space:]]*$/)
imports_state = "started"
if (exports_state == "started" &&
line ~ /^([[:space:]]*\*[[:space:]]+)?EXPORT[[:space:]]+[_a-zA-Z][_a-zA-Z0-9]*[[:space:]]*$/) {
record = line
sub(/^([[:space:]]*\*[[:space:]]+)?EXPORT[[:space:]]+/, "", record)
sub(/([[:space:]]+$)/, "", record)
if (record in exports) {
printf "ERROR: '%s' exported by both %s and %s\n",
exports[record], filename > "/dev/stderr"
}
provides[record] = filename
exports[filename,++export_counts[filename]] = record
}
if (exports_state == "started" &&
line ~ /^([[:space:]]*\*[[:space:]]+)?EXPORTS_END[[:space:]]*$/)
exports_state = "finished"
if (exports_state == "not_started" &&
line ~ /^([[:space:]]*\*[[:space:]]+)?EXPORTS_START[[:space:]]*$/)
exports_state = "started"
} while (result > 0)
if (imports_state == "started") {
printf "ERROR: Unclosed IMPORTS list in '%s'\n", filename \
> "/dev/stderr"
exit 1
}
if (exports_state == "started") {
printf "ERROR: Unclosed EXPORTS list in '%s'\n", filename \
> "/dev/stderr"
exit 1
}
close(filename)
}
function print_file(filename, line) {
while ((getline line < filename) > 0)
print(line)
close(filename)
}
function print_imports_code(filename, i, count, import_name) {
count = import_counts[filename]
for (i = 1; i <= count; i++) {
import_name = imports[filename,i]
printf "const %s = window.haketilo_exports.%s;\n",
import_name, import_name
}
}
function print_exports_code(filename, i, count, export_name) {
count = export_counts[filename]
for (i = 1; i <= count; i++) {
export_name = exports[filename,i]
printf "window.haketilo_exports.%s = %s;\n", export_name, export_name
}
}
function partially_wrap_file(filename) {
print_imports_code(filename)
printf "\n\n"
print_file(filename)
printf "\n\n"
print_exports_code(filename)
}
function wrap_file(filename) {
print "\"use strict\";\n\n({fun: (function() {\n"
partially_wrap_file(filename)
print "\n})}).fun();"
}
function compute_dependencies(filename, i, count, import_name, next_file) {
if (processed[filename] == "used")
return 0
if (processed[filename] == "on_stack") {
printf "import loop on %s\n", filename > "/dev/stderr"
return 1
}
processed[filename] = "on_stack"
count = import_counts[filename]
for (i = 1; i <= count; i++) {
import_name = imports[filename,i]
if (!(import_name in provides)) {
printf "nothing exports %s, required by %s\n",
import_name, filename > "/dev/stderr"
return 1
}
if (compute_dependencies(provides[import_name]) > 0) {
printf "when satisfying %s for %s\n",
import_name, filename > "/dev/stderr"
return 1
}
}
processed[filename] = "used"
print filename
return 0
}
function print_usage() {
printf "usage: %2 compute_scripts.awk script_dependencies|wrapped_code|partially_wrapped_code FILENAME[...]\n",
ARGV[0] > "/dev/stderr"
exit 1
}
function mock_exports_init() {
provides["browser"] = "exports_init.js"
provides["is_chrome"] = "exports_init.js"
provides["is_mozilla"] = "exports_init.js"
processed["exports_init.js"] = "used"
}
BEGIN {
operation = ARGV[1]
if (ARGC < 3)
print_usage()
root_filename = ARGV[2]
for (i = 2; i < ARGC; i++)
filenames[ARGV[i]]
mock_exports_init()
for (filename in filenames) {
# A filename is allowed to appear multiple times in the list.
# Let's only process it once.
if (!(filename in processed))
read_file(filename)
processed[filename] = "not_used"
}
if (operation == "script_dependencies") {
print("exports_init.js")
if (compute_dependencies(root_filename) > 0)
exit 1
} else if (operation == "partially_wrapped_code") {
partially_wrap_file(root_filename)
} else if (operation == "wrapped_code") {
wrap_file(root_filename)
} else {
print_usage()
}
}
|