MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / emit_lua

Function emit_lua

scripts/generate-sdk.py:237–349  ·  view source on GitHub ↗
(commands)

Source from the content-addressed store, hash-verified

235
236
237def emit_lua(commands):
238 lines = []
239 lines.append("-- AUTO-GENERATED by scripts/generate-sdk.py. Do not edit by hand.")
240 lines.append(
241 "-- Natural wrappers over apiCall(method, params). Refresh with sanitize-commit."
242 )
243 lines.append("--")
244 lines.append(
245 "-- apiCall/apiCallList are installed natively by the host (they capture the"
246 )
247 lines.append(
248 "-- per-source id in a C closure, so they cannot be pure Lua). Everything else"
249 )
250 lines.append("-- in the Serial Studio scripting surface is defined below.")
251 lines.append("")
252 lines.append("SerialStudio = { Base64 = 0, Hex = 1, Text = 2 }")
253 lines.append("")
254 lines.append(
255 "local _b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'"
256 )
257 lines.append("")
258 lines.append(
259 "-- Encodes a raw byte string as base64 (pure Lua, no host dependency)."
260 )
261 lines.append("local function _toBase64(bytes)")
262 lines.append(" local out, n = {}, #bytes")
263 lines.append(" for i = 1, n, 3 do")
264 lines.append(" local b1 = bytes:byte(i) or 0")
265 lines.append(" local b2 = bytes:byte(i + 1)")
266 lines.append(" local b3 = bytes:byte(i + 2)")
267 lines.append(" local n1 = b1 >> 2")
268 lines.append(" local n2 = ((b1 & 3) << 4) | ((b2 or 0) >> 4)")
269 lines.append(" local n3 = ((((b2 or 0) & 15) << 2) | ((b3 or 0) >> 6))")
270 lines.append(" local n4 = (b3 or 0) & 63")
271 lines.append(" out[#out + 1] = _b64:sub(n1 + 1, n1 + 1)")
272 lines.append(" out[#out + 1] = _b64:sub(n2 + 1, n2 + 1)")
273 lines.append(" out[#out + 1] = b2 and _b64:sub(n3 + 1, n3 + 1) or '='")
274 lines.append(" out[#out + 1] = b3 and _b64:sub(n4 + 1, n4 + 1) or '='")
275 lines.append(" end")
276 lines.append(" return table.concat(out)")
277 lines.append("end")
278 lines.append("")
279 lines.append("function SerialStudio._encode(value, encoding)")
280 lines.append(" if encoding == SerialStudio.Hex then")
281 lines.append(" local hex = tostring(value):gsub('%X', '')")
282 lines.append(" local bin = {}")
283 lines.append(" for i = 1, #hex, 2 do")
284 lines.append(
285 " bin[#bin + 1] = string.char(tonumber(hex:sub(i, i + 1), 16) or 0)"
286 )
287 lines.append(" end")
288 lines.append(" return _toBase64(table.concat(bin))")
289 lines.append(" elseif encoding == SerialStudio.Text then")
290 lines.append(" return _toBase64(tostring(value))")
291 lines.append(" end")
292 lines.append(" return value")
293 lines.append("end")
294 lines.append("")

Callers 1

mainFunction · 0.85

Calls 7

read_preludeFunction · 0.85
ordered_paramsFunction · 0.85
is_byte_paramFunction · 0.85
setFunction · 0.50
appendMethod · 0.45
addMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected