@overmvp/kumi 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +334 -0
- package/builder/css-v1.test.mjs +321 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +86 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +444 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2164 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +367 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +47 -0
- package/builder/src/numbers.lua +62 -0
- package/builder/src/project-front.mjs +3331 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +127 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +17 -0
- package/form/index.ts +522 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +22 -4
- package/router/index.ts +43 -0
- package/router/package.json +12 -0
- package/router/routes.ts +64 -0
- package/scripts/kumi-cli.mjs +70 -0
- package/scripts/link-addons.mjs +51 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +234 -0
- package/src/css-v1.ts +58 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +479 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +334 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +28 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +22 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +363 -0
- package/src/media-query.ts +144 -0
- package/src/module.ts +277 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +44 -0
- package/src/providers.ts +235 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +254 -0
- package/src/result.ts +58 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +30 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +3 -0
- package/state/package.json +12 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +82 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/README.md +14 -0
- package/templates/addon/__ADDON_NAME__.toc +4 -0
- package/templates/addon/eslint.config.mjs +12 -0
- package/templates/addon/gitignore +8 -0
- package/templates/addon/package.json +21 -0
- package/templates/addon/scripts/build.mjs +25 -0
- package/templates/addon/src/app/main.tsx +13 -0
- package/templates/addon/tsconfig.json +24 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/animation.ts +428 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/css-box.ts +110 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +398 -0
- package/wow/css-text.ts +399 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +31 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +97 -0
- package/wow/layout-view.ts +651 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +135 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/protect.ts +24 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/range-art.ts +74 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +156 -0
- package/wow/saved-variables.ts +60 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/start-addon.ts +99 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1746 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
use image::codecs::png::PngEncoder;
|
|
2
|
+
use image::{
|
|
3
|
+
DynamicImage, ExtendedColorType, ImageDecoder, ImageEncoder, ImageFormat, ImageReader,
|
|
4
|
+
RgbaImage,
|
|
5
|
+
};
|
|
6
|
+
use std::fs;
|
|
7
|
+
use std::io::{self, Cursor, Read};
|
|
8
|
+
use std::path::Path;
|
|
9
|
+
|
|
10
|
+
pub(crate) const MAX_TEXTURE_SIZE: u32 = 4096;
|
|
11
|
+
|
|
12
|
+
/// An uncompressed 32-bit TGA with its origin at the bottom left (descriptor 0x08), the layout of every TGA the
|
|
13
|
+
/// client is known to read (2026-11-22): the `image` crate writes a top-left origin, which not every reader honors.
|
|
14
|
+
fn encode_tga(image: &RgbaImage) -> Vec<u8> {
|
|
15
|
+
let (width, height) = image.dimensions();
|
|
16
|
+
let mut bytes = Vec::with_capacity(18 + (width * height * 4) as usize);
|
|
17
|
+
bytes.extend_from_slice(&[0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
18
|
+
bytes.extend_from_slice(&(width as u16).to_le_bytes());
|
|
19
|
+
bytes.extend_from_slice(&(height as u16).to_le_bytes());
|
|
20
|
+
bytes.extend_from_slice(&[32, 0x08]);
|
|
21
|
+
for y in (0..height).rev() {
|
|
22
|
+
for x in 0..width {
|
|
23
|
+
let [r, g, b, a] = image.get_pixel(x, y).0;
|
|
24
|
+
bytes.extend_from_slice(&[b, g, r, a]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
bytes
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fn publish(path: &Path, bytes: &[u8], check: bool) -> io::Result<()> {
|
|
31
|
+
if fs::read(path).ok().as_deref() == Some(bytes) {
|
|
32
|
+
return Ok(());
|
|
33
|
+
}
|
|
34
|
+
if check {
|
|
35
|
+
return Err(io::Error::other(format!(
|
|
36
|
+
"{} is stale; run the UI build",
|
|
37
|
+
path.display()
|
|
38
|
+
)));
|
|
39
|
+
}
|
|
40
|
+
let parent = path
|
|
41
|
+
.parent()
|
|
42
|
+
.filter(|parent| !parent.as_os_str().is_empty())
|
|
43
|
+
.unwrap_or(Path::new("."));
|
|
44
|
+
fs::create_dir_all(parent)?;
|
|
45
|
+
let temp = path.with_extension(format!("{}.tmp", std::process::id()));
|
|
46
|
+
fs::write(&temp, bytes)?;
|
|
47
|
+
match fs::rename(&temp, path) {
|
|
48
|
+
Ok(()) => Ok(()),
|
|
49
|
+
Err(error) => {
|
|
50
|
+
let _ = fs::remove_file(&temp);
|
|
51
|
+
if fs::read(path).ok().as_deref() == Some(bytes) {
|
|
52
|
+
Ok(())
|
|
53
|
+
} else {
|
|
54
|
+
Err(error)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn is_direct_tga(data: &[u8], width: u32, height: u32) -> bool {
|
|
61
|
+
data.get(1..3) == Some(&[0, 2])
|
|
62
|
+
&& matches!(data.get(16), Some(24 | 32))
|
|
63
|
+
&& width.is_power_of_two()
|
|
64
|
+
&& height.is_power_of_two()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub fn direct_tga_dimensions(input: &Path) -> Result<(u32, u32), Box<dyn std::error::Error>> {
|
|
68
|
+
let data = fs::read(input)?;
|
|
69
|
+
let decoder = ImageReader::with_format(Cursor::new(&data), ImageFormat::Tga).into_decoder()?;
|
|
70
|
+
let (width, height) = decoder.dimensions();
|
|
71
|
+
if width > MAX_TEXTURE_SIZE || height > MAX_TEXTURE_SIZE || !is_direct_tga(&data, width, height)
|
|
72
|
+
{
|
|
73
|
+
return Err(format!(
|
|
74
|
+
"{}: expected a direct power-of-two WoW TGA",
|
|
75
|
+
input.display()
|
|
76
|
+
)
|
|
77
|
+
.into());
|
|
78
|
+
}
|
|
79
|
+
DynamicImage::from_decoder(decoder)?;
|
|
80
|
+
Ok((width, height))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
pub fn build(
|
|
84
|
+
input: &Path,
|
|
85
|
+
output: &Path,
|
|
86
|
+
check: bool,
|
|
87
|
+
tga_input: bool,
|
|
88
|
+
) -> Result<(), Box<dyn std::error::Error>> {
|
|
89
|
+
let mut data = Vec::new();
|
|
90
|
+
if input == Path::new("-") {
|
|
91
|
+
io::stdin().lock().read_to_end(&mut data)?;
|
|
92
|
+
} else {
|
|
93
|
+
data = fs::read(input)?;
|
|
94
|
+
}
|
|
95
|
+
let is_tga = tga_input
|
|
96
|
+
|| input
|
|
97
|
+
.extension()
|
|
98
|
+
.is_some_and(|extension| extension.eq_ignore_ascii_case("tga"));
|
|
99
|
+
let source = Cursor::new(&data);
|
|
100
|
+
let mut decoder = if is_tga {
|
|
101
|
+
ImageReader::with_format(source, ImageFormat::Tga)
|
|
102
|
+
} else {
|
|
103
|
+
ImageReader::new(source).with_guessed_format()?
|
|
104
|
+
}
|
|
105
|
+
.into_decoder()?;
|
|
106
|
+
let (width, height) = decoder.dimensions();
|
|
107
|
+
if width == 0 || height == 0 || width > MAX_TEXTURE_SIZE || height > MAX_TEXTURE_SIZE {
|
|
108
|
+
return Err(format!(
|
|
109
|
+
"{}: image dimensions {width}x{height} exceed the 1–{MAX_TEXTURE_SIZE} texture range",
|
|
110
|
+
input.display()
|
|
111
|
+
)
|
|
112
|
+
.into());
|
|
113
|
+
}
|
|
114
|
+
let orientation = decoder.orientation()?;
|
|
115
|
+
let mut image = DynamicImage::from_decoder(decoder)?;
|
|
116
|
+
let png_output = output
|
|
117
|
+
.extension()
|
|
118
|
+
.is_some_and(|extension| extension.eq_ignore_ascii_case("png"));
|
|
119
|
+
if is_tga && !png_output && is_direct_tga(&data, width, height) {
|
|
120
|
+
publish(output, &data, check)?;
|
|
121
|
+
println!("{width} {height} {width} {height}");
|
|
122
|
+
return Ok(());
|
|
123
|
+
}
|
|
124
|
+
image.apply_orientation(orientation);
|
|
125
|
+
let image = image.into_rgba8();
|
|
126
|
+
let (width, height) = image.dimensions();
|
|
127
|
+
if png_output {
|
|
128
|
+
let mut png = Vec::new();
|
|
129
|
+
PngEncoder::new(&mut png).write_image(
|
|
130
|
+
image.as_raw(),
|
|
131
|
+
width,
|
|
132
|
+
height,
|
|
133
|
+
ExtendedColorType::Rgba8,
|
|
134
|
+
)?;
|
|
135
|
+
publish(output, &png, check)?;
|
|
136
|
+
println!("{width} {height} {width} {height}");
|
|
137
|
+
return Ok(());
|
|
138
|
+
}
|
|
139
|
+
let (texture_width, texture_height) = (width.next_power_of_two(), height.next_power_of_two());
|
|
140
|
+
let texture = if (width, height) == (texture_width, texture_height) {
|
|
141
|
+
image
|
|
142
|
+
} else {
|
|
143
|
+
let mut padded = RgbaImage::new(texture_width, texture_height);
|
|
144
|
+
image::imageops::replace(&mut padded, &image, 0, 0);
|
|
145
|
+
padded
|
|
146
|
+
};
|
|
147
|
+
let tga = encode_tga(&texture);
|
|
148
|
+
publish(output, &tga, check)?;
|
|
149
|
+
println!("{width} {height} {texture_width} {texture_height}");
|
|
150
|
+
Ok(())
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#[cfg(test)]
|
|
154
|
+
mod tests {
|
|
155
|
+
use super::*;
|
|
156
|
+
|
|
157
|
+
#[test]
|
|
158
|
+
fn tga_rows_run_bottom_up_in_bgra() {
|
|
159
|
+
let mut image = RgbaImage::new(2, 2);
|
|
160
|
+
image.put_pixel(0, 0, image::Rgba([1, 2, 3, 4]));
|
|
161
|
+
image.put_pixel(1, 1, image::Rgba([5, 6, 7, 8]));
|
|
162
|
+
let tga = encode_tga(&image);
|
|
163
|
+
assert_eq!(
|
|
164
|
+
&tga[..18],
|
|
165
|
+
&[0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 32, 8]
|
|
166
|
+
);
|
|
167
|
+
// The bottom row comes first: (0, 1) then (1, 1); then the top row (0, 0), (1, 0).
|
|
168
|
+
assert_eq!(
|
|
169
|
+
&tga[18..],
|
|
170
|
+
&[0, 0, 0, 0, 7, 6, 5, 8, 3, 2, 1, 4, 0, 0, 0, 0]
|
|
171
|
+
);
|
|
172
|
+
let decoded = image::load_from_memory_with_format(&tga, ImageFormat::Tga)
|
|
173
|
+
.unwrap()
|
|
174
|
+
.into_rgba8();
|
|
175
|
+
assert_eq!(decoded, image);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
local classPrototype = function(methods, getters, setters, base)
|
|
2
|
+
if base then
|
|
3
|
+
local ancestors = { [base] = true }
|
|
4
|
+
if base.kumiAncestors then
|
|
5
|
+
for ancestor in pairs(base.kumiAncestors) do ancestors[ancestor] = true end
|
|
6
|
+
end
|
|
7
|
+
return {
|
|
8
|
+
kumiAncestors = ancestors,
|
|
9
|
+
__index = function(self, key)
|
|
10
|
+
local getter = getters[key]
|
|
11
|
+
if getter then return (getter(self)) end
|
|
12
|
+
local method = rawget(methods, key)
|
|
13
|
+
if method ~= nil or setters[key] ~= nil then return method end
|
|
14
|
+
return base.__index(self, key)
|
|
15
|
+
end,
|
|
16
|
+
__newindex = function(self, key, value)
|
|
17
|
+
local setter = setters[key]
|
|
18
|
+
if setter then
|
|
19
|
+
setter(self, value)
|
|
20
|
+
elseif getters[key] then
|
|
21
|
+
error("Cannot assign to the read-only property " .. key, 2)
|
|
22
|
+
elseif rawget(methods, key) ~= nil then
|
|
23
|
+
rawset(self, key, value)
|
|
24
|
+
else
|
|
25
|
+
base.__newindex(self, key, value)
|
|
26
|
+
end
|
|
27
|
+
end,
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
return {
|
|
31
|
+
__index = function(self, key)
|
|
32
|
+
local getter = getters[key]
|
|
33
|
+
if getter then return (getter(self)) end
|
|
34
|
+
return methods[key]
|
|
35
|
+
end,
|
|
36
|
+
__newindex = function(self, key, value)
|
|
37
|
+
local setter = setters[key]
|
|
38
|
+
if setter then
|
|
39
|
+
setter(self, value)
|
|
40
|
+
elseif getters[key] then
|
|
41
|
+
error("Cannot assign to the read-only property " .. key, 2)
|
|
42
|
+
else
|
|
43
|
+
rawset(self, key, value)
|
|
44
|
+
end
|
|
45
|
+
end,
|
|
46
|
+
}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
local function classValue(construct, prototype, methods, statics, name, base)
|
|
50
|
+
local class = setmetatable(statics, { __call = function(_, ...) return construct(nil, ...) end, __index = base })
|
|
51
|
+
class.name = name
|
|
52
|
+
class.prototype = methods
|
|
53
|
+
methods.constructor = class
|
|
54
|
+
if base then setmetatable(methods, { __index = base.prototype }) end
|
|
55
|
+
prototype.kumiConstruct = construct
|
|
56
|
+
prototype.kumiClass = class
|
|
57
|
+
classPrototypes[class] = prototype
|
|
58
|
+
return class
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
local function constructClass(class, ...)
|
|
62
|
+
if not classPrototypes[class] then error("Expected a compiled Kumi class", 2) end
|
|
63
|
+
return class(...)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
local function classInstance(value, class)
|
|
67
|
+
if type(value) ~= "table" then return false end
|
|
68
|
+
local expected = classPrototypes[class]
|
|
69
|
+
if expected == nil then return false end
|
|
70
|
+
local actual = getmetatable(value)
|
|
71
|
+
if actual == expected then return true end
|
|
72
|
+
if type(actual) ~= "table" or classPrototypes[rawget(actual, "kumiClass")] ~= actual then return false end
|
|
73
|
+
local ancestors = actual.kumiAncestors
|
|
74
|
+
return ancestors ~= nil and ancestors[expected] == true
|
|
75
|
+
end
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
do
|
|
2
|
+
local nilKey, nanKey = {}, {}
|
|
3
|
+
local weakKeys = { __mode = "k" }
|
|
4
|
+
|
|
5
|
+
local function keyOf(value)
|
|
6
|
+
if value == nil then return nilKey end
|
|
7
|
+
if type(value) == "number" and value ~= value then return nanKey end
|
|
8
|
+
return value
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
local function has(self, key)
|
|
12
|
+
return self.entries[keyOf(key)] ~= nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
local function get(self, key)
|
|
16
|
+
local entry = self.entries[keyOf(key)]
|
|
17
|
+
if entry then return entry.value end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
local function put(self, key, value)
|
|
21
|
+
local index = keyOf(key)
|
|
22
|
+
local existing = self.entries[index]
|
|
23
|
+
if existing then
|
|
24
|
+
existing.value = value
|
|
25
|
+
return self
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
local tail = self.tail
|
|
29
|
+
local entry = { key = key, value = value, previous = tail }
|
|
30
|
+
if tail then tail.following = entry else self.head = entry end
|
|
31
|
+
self.tail = entry
|
|
32
|
+
self.entries[index] = entry
|
|
33
|
+
self.size = self.size + 1
|
|
34
|
+
-- Iteration that has yielded the old tail can see a later insertion.
|
|
35
|
+
if self.cursors then
|
|
36
|
+
for cursor in pairs(self.cursors) do
|
|
37
|
+
if cursor.pending == nil then cursor.pending = entry end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
return self
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
local function delete(self, key)
|
|
44
|
+
local index = keyOf(key)
|
|
45
|
+
local entry = self.entries[index]
|
|
46
|
+
if not entry then return false end
|
|
47
|
+
|
|
48
|
+
local previous, following = entry.previous, entry.following
|
|
49
|
+
if previous then previous.following = following else self.head = following end
|
|
50
|
+
if following then following.previous = previous else self.tail = previous end
|
|
51
|
+
if self.cursors then
|
|
52
|
+
for cursor in pairs(self.cursors) do
|
|
53
|
+
if cursor.pending == entry then cursor.pending = following end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
self.entries[index] = nil
|
|
57
|
+
self.size = self.size - 1
|
|
58
|
+
return true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
local function clear(self)
|
|
62
|
+
self.entries, self.head, self.tail, self.size = {}, nil, nil, 0
|
|
63
|
+
if self.cursors then
|
|
64
|
+
for cursor in pairs(self.cursors) do cursor.pending = nil end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
local map = { __index = { get = get, has = has, set = put, delete = delete, clear = clear } }
|
|
69
|
+
local set = { __index = { has = has, add = put, delete = delete, clear = clear } }
|
|
70
|
+
|
|
71
|
+
local nilWeakValue = {}
|
|
72
|
+
local function weakKey(key)
|
|
73
|
+
local kind = type(key)
|
|
74
|
+
return kind == "table" or kind == "function" or kind == "userdata"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
local weakmap = { __index = {
|
|
78
|
+
get = function(self, key)
|
|
79
|
+
if not weakKey(key) then return end
|
|
80
|
+
local value = self.values[key]
|
|
81
|
+
if value ~= nilWeakValue then return value end
|
|
82
|
+
end,
|
|
83
|
+
has = function(self, key)
|
|
84
|
+
return weakKey(key) and self.values[key] ~= nil
|
|
85
|
+
end,
|
|
86
|
+
set = function(self, key, value)
|
|
87
|
+
if not weakKey(key) then error("WeakMap key must have identity", 2) end
|
|
88
|
+
if value == nil then value = nilWeakValue end
|
|
89
|
+
self.values[key] = value
|
|
90
|
+
return self
|
|
91
|
+
end,
|
|
92
|
+
delete = function(self, key)
|
|
93
|
+
if not weakKey(key) or self.values[key] == nil then return false end
|
|
94
|
+
self.values[key] = nil
|
|
95
|
+
return true
|
|
96
|
+
end,
|
|
97
|
+
} }
|
|
98
|
+
|
|
99
|
+
local function step(cursor)
|
|
100
|
+
local entry = cursor.pending
|
|
101
|
+
if not entry then
|
|
102
|
+
cursor.owner.cursors[cursor] = nil
|
|
103
|
+
return
|
|
104
|
+
end
|
|
105
|
+
cursor.pending = entry.following
|
|
106
|
+
return entry, entry.key, entry.value
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
local function stepEntry(cursor)
|
|
110
|
+
local entry, key, value = step(cursor)
|
|
111
|
+
if entry then return entry, { key, value } end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
local function nextEntry(iterator)
|
|
115
|
+
local entry, record = stepEntry(iterator.cursor)
|
|
116
|
+
if entry then return { done = false, value = record } end
|
|
117
|
+
return { done = true }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
collections = {
|
|
121
|
+
map = function() return setmetatable({ size = 0, entries = {} }, map) end,
|
|
122
|
+
set = function() return setmetatable({ size = 0, entries = {} }, set) end,
|
|
123
|
+
weakmap = function() return setmetatable({ values = setmetatable({}, weakKeys) }, weakmap) end,
|
|
124
|
+
snapshot = function(owner, field)
|
|
125
|
+
local values, entry = {}, owner.head
|
|
126
|
+
while entry do
|
|
127
|
+
local value = field == "entry" and { entry.key, entry.value } or entry[field]
|
|
128
|
+
if value == nil then error("Cannot spread nil into a dense Kumi array", 2) end
|
|
129
|
+
values[#values + 1] = value
|
|
130
|
+
entry = entry.following
|
|
131
|
+
end
|
|
132
|
+
return values
|
|
133
|
+
end,
|
|
134
|
+
iterate = function(owner)
|
|
135
|
+
if not owner.cursors then owner.cursors = setmetatable({}, weakKeys) end
|
|
136
|
+
local cursor = { owner = owner, pending = owner.head }
|
|
137
|
+
owner.cursors[cursor] = true
|
|
138
|
+
return step, cursor
|
|
139
|
+
end,
|
|
140
|
+
mapToArray = function(owner, mapValue, mapEntries)
|
|
141
|
+
local result, index = {}, 0
|
|
142
|
+
for _, key, value in collections.iterate(owner) do
|
|
143
|
+
local item = key
|
|
144
|
+
if mapEntries then item = { key, value } end
|
|
145
|
+
index = index + 1
|
|
146
|
+
result[index] = mapValue(item, index - 1)
|
|
147
|
+
end
|
|
148
|
+
return result
|
|
149
|
+
end,
|
|
150
|
+
entryIterator = function(owner)
|
|
151
|
+
local _, cursor = collections.iterate(owner)
|
|
152
|
+
return { cursor = cursor, next = nextEntry }
|
|
153
|
+
end,
|
|
154
|
+
iterateEntries = function(iterator) return step, iterator.cursor end,
|
|
155
|
+
iterateEntryRecords = function(iterator) return stepEntry, iterator.cursor end,
|
|
156
|
+
values = function(owner)
|
|
157
|
+
if getmetatable(owner) == set then return collections.iterate(owner) end
|
|
158
|
+
return ipairs(owner)
|
|
159
|
+
end,
|
|
160
|
+
copyMap = function(source)
|
|
161
|
+
if getmetatable(source) ~= map then error("Expected a native Kumi Map", 2) end
|
|
162
|
+
local result = collections.map()
|
|
163
|
+
for _, key, value in collections.iterate(source) do put(result, key, value) end
|
|
164
|
+
return result
|
|
165
|
+
end,
|
|
166
|
+
copySet = function(source)
|
|
167
|
+
local result = collections.set()
|
|
168
|
+
for _, value in collections.iterate(source) do put(result, value) end
|
|
169
|
+
return result
|
|
170
|
+
end,
|
|
171
|
+
mapFromArray = function(source)
|
|
172
|
+
local result = collections.map()
|
|
173
|
+
for index = 1, #source do
|
|
174
|
+
local pair = source[index]
|
|
175
|
+
put(result, pair[1], pair[2])
|
|
176
|
+
end
|
|
177
|
+
return result
|
|
178
|
+
end,
|
|
179
|
+
setFromArray = function(source)
|
|
180
|
+
local result = collections.set()
|
|
181
|
+
for index = 1, #source do put(result, source[index]) end
|
|
182
|
+
return result
|
|
183
|
+
end,
|
|
184
|
+
setFromLuaArray = function(source)
|
|
185
|
+
local result = collections.set()
|
|
186
|
+
for _, value in ipairs(source) do put(result, value) end
|
|
187
|
+
return result
|
|
188
|
+
end,
|
|
189
|
+
}
|
|
190
|
+
end
|