Skip to content

Commit 75fdba5

Browse files
committed
Fix all credo warnings, design, and readability issues
- Add module aliases across all files, remove fully-qualified refs - Fix alias ordering (alphabetical) in builtin, bytecode, property, map_set - Convert explicit try to implicit try in generator, interpreter, json, object - Replace with-single-clause with case in date.ex - Add credo:disable for JS API predicate names (is_nan, is_finite, is_array) - Fix number formatting: 146097 → 146_097, 65520.0 → 65_520.0 - Split semicolon one-liners into multiline in array_buffer, object - Fix line length in typed_array - Replace length/1 comparisons with pattern matching in tests - Add @doc strings on public interpreter functions - Remove dead bound_fn clause in new handler - Remove trailing whitespace
1 parent 976090d commit 75fdba5

21 files changed

Lines changed: 221 additions & 171 deletions

lib/quickbeam.ex

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
defmodule QuickBEAM do
22
import QuickBEAM.BeamVM.Heap.Keys
33

4+
alias QuickBEAM.BeamVM.Bytecode, as: BeamBytecode
5+
alias QuickBEAM.BeamVM.Heap
6+
alias QuickBEAM.BeamVM.Interpreter
7+
alias QuickBEAM.BeamVM.Interpreter.Promise
8+
alias QuickBEAM.BeamVM.Runtime, as: BeamRuntime
9+
alias QuickBEAM.Bytecode
10+
alias QuickBEAM.JSError
11+
alias QuickBEAM.Native
12+
alias QuickBEAM.Runtime
13+
414
@moduledoc """
515
QuickJS-NG JavaScript engine embedded in the BEAM.
616
@@ -60,7 +70,7 @@ defmodule QuickBEAM do
6070

6171
@doc false
6272
def child_spec(opts) do
63-
QuickBEAM.Runtime.child_spec(opts)
73+
Runtime.child_spec(opts)
6474
end
6575

6676
@doc """
@@ -100,7 +110,7 @@ defmodule QuickBEAM do
100110
end
101111
end
102112

103-
QuickBEAM.Runtime.start_link(opts)
113+
Runtime.start_link(opts)
104114
end
105115

106116
@doc """
@@ -142,14 +152,14 @@ defmodule QuickBEAM do
142152
if resolve_mode(runtime, opts) == :beam do
143153
eval_beam(runtime, code, opts)
144154
else
145-
QuickBEAM.Runtime.eval(runtime, code, opts)
155+
Runtime.eval(runtime, code, opts)
146156
end
147157
end
148158

149159
defp resolve_mode(runtime, opts) do
150160
case Keyword.get(opts, :mode) do
151161
nil ->
152-
case QuickBEAM.BeamVM.Heap.get_runtime_mode(runtime) do
162+
case Heap.get_runtime_mode(runtime) do
153163
nil ->
154164
mode =
155165
try do
@@ -158,7 +168,7 @@ defmodule QuickBEAM do
158168
:exit, _ -> :nif
159169
end
160170

161-
QuickBEAM.BeamVM.Heap.put_runtime_mode(runtime, mode)
171+
Heap.put_runtime_mode(runtime, mode)
162172
mode
163173

164174
cached ->
@@ -171,10 +181,8 @@ defmodule QuickBEAM do
171181
end
172182

173183
defp eval_beam(runtime, code, _opts) do
174-
alias QuickBEAM.BeamVM.{Bytecode, Interpreter}
175-
176184
handler_globals =
177-
case QuickBEAM.BeamVM.Heap.get_handler_globals() do
185+
case Heap.get_handler_globals() do
178186
nil ->
179187
handlers =
180188
try do
@@ -196,16 +204,16 @@ defmodule QuickBEAM do
196204
end}}
197205
end
198206

199-
QuickBEAM.BeamVM.Heap.put_handler_globals(globals)
207+
Heap.put_handler_globals(globals)
200208
globals
201209

202210
cached ->
203211
cached
204212
end
205213

206-
case QuickBEAM.Runtime.compile(runtime, code) do
214+
case Runtime.compile(runtime, code) do
207215
{:ok, bc} ->
208-
case Bytecode.decode(bc) do
216+
case BeamBytecode.decode(bc) do
209217
{:ok, parsed} ->
210218
result =
211219
Interpreter.eval(
@@ -215,9 +223,9 @@ defmodule QuickBEAM do
215223
parsed.atoms
216224
)
217225

218-
QuickBEAM.BeamVM.Interpreter.Promise.drain_microtasks()
226+
Promise.drain_microtasks()
219227
converted = convert_beam_result(result)
220-
QuickBEAM.BeamVM.Heap.gc()
228+
Heap.gc()
221229
converted
222230

223231
{:error, _} = err ->
@@ -245,18 +253,18 @@ defmodule QuickBEAM do
245253
defp convert_beam_result({:ok, val}), do: {:ok, convert_beam_value(val)}
246254
defp convert_beam_result({:error, _} = err), do: err
247255

248-
defp wrap_js_error(val), do: QuickBEAM.JSError.from_js_value(val)
256+
defp wrap_js_error(val), do: JSError.from_js_value(val)
249257

250258
defp elixir_to_js(val) when is_map(val) do
251259
ref = make_ref()
252260
obj = Map.new(val, fn {k, v} -> {to_string(k), elixir_to_js(v)} end)
253-
QuickBEAM.BeamVM.Heap.put_obj(ref, obj)
261+
Heap.put_obj(ref, obj)
254262
{:obj, ref}
255263
end
256264

257265
defp elixir_to_js(val) when is_list(val) do
258266
ref = make_ref()
259-
QuickBEAM.BeamVM.Heap.put_obj(ref, Enum.map(val, &elixir_to_js/1))
267+
Heap.put_obj(ref, Enum.map(val, &elixir_to_js/1))
260268
{:obj, ref}
261269
end
262270

@@ -265,7 +273,7 @@ defmodule QuickBEAM do
265273
defp convert_beam_value(:undefined), do: nil
266274

267275
defp convert_beam_value({:obj, ref}) do
268-
case QuickBEAM.BeamVM.Heap.get_obj(ref) do
276+
case Heap.get_obj(ref) do
269277
nil ->
270278
nil
271279

@@ -290,15 +298,13 @@ defmodule QuickBEAM do
290298
defp convert_beam_key(k), do: inspect(k)
291299

292300
defp load_module_beam(runtime, name, code) do
293-
alias QuickBEAM.BeamVM.{Bytecode, Interpreter, Heap}
294-
295301
wrapper =
296302
"(function() { var module = {exports: {}}; var exports = module.exports; " <>
297303
code <> "; return module.exports })()"
298304

299-
case QuickBEAM.Runtime.compile(runtime, wrapper) do
305+
case Runtime.compile(runtime, wrapper) do
300306
{:ok, bc} ->
301-
case Bytecode.decode(bc) do
307+
case BeamBytecode.decode(bc) do
302308
{:ok, parsed} ->
303309
case Interpreter.eval(
304310
parsed.value,
@@ -348,24 +354,22 @@ defmodule QuickBEAM do
348354
if resolve_mode(runtime, opts) == :beam do
349355
call_beam(runtime, fn_name, args)
350356
else
351-
QuickBEAM.Runtime.call(runtime, fn_name, args, opts)
357+
Runtime.call(runtime, fn_name, args, opts)
352358
end
353359
end
354360

355361
defp call_beam(_runtime, fn_name, args) do
356-
alias QuickBEAM.BeamVM.{Interpreter, Heap, Runtime}
357-
358-
handler_globals = QuickBEAM.BeamVM.Heap.get_handler_globals() || %{}
362+
handler_globals = Heap.get_handler_globals() || %{}
359363

360364
globals =
361-
Runtime.global_bindings()
365+
BeamRuntime.global_bindings()
362366
|> Map.merge(handler_globals)
363-
|> Map.merge(QuickBEAM.BeamVM.Heap.get_persistent_globals())
367+
|> Map.merge(Heap.get_persistent_globals())
364368

365369
case Map.get(globals, fn_name) do
366370
nil ->
367371
{:error,
368-
QuickBEAM.JSError.from_js_value(%{
372+
JSError.from_js_value(%{
369373
"message" => "#{fn_name} is not defined",
370374
"name" => "ReferenceError"
371375
})}
@@ -395,8 +399,8 @@ defmodule QuickBEAM do
395399
"""
396400
@spec disasm(binary()) :: {:ok, QuickBEAM.Bytecode.t()} | {:error, String.t()}
397401
def disasm(bytecode) when is_binary(bytecode) do
398-
case QuickBEAM.Native.disasm_bytecode(bytecode) do
399-
{:ok, map} -> {:ok, QuickBEAM.Bytecode.from_map(map)}
402+
case Native.disasm_bytecode(bytecode) do
403+
{:ok, map} -> {:ok, Bytecode.from_map(map)}
400404
{:error, _} = error -> error
401405
end
402406
end
@@ -423,7 +427,7 @@ defmodule QuickBEAM do
423427
"""
424428
@spec compile(runtime(), String.t()) :: {:ok, binary()} | {:error, QuickBEAM.JSError.t()}
425429
def compile(runtime, code) do
426-
QuickBEAM.Runtime.compile(runtime, code)
430+
Runtime.compile(runtime, code)
427431
end
428432

429433
@doc """
@@ -434,7 +438,7 @@ defmodule QuickBEAM do
434438
"""
435439
@spec load_bytecode(runtime(), binary()) :: js_result()
436440
def load_bytecode(runtime, bytecode) do
437-
QuickBEAM.Runtime.load_bytecode(runtime, bytecode)
441+
Runtime.load_bytecode(runtime, bytecode)
438442
end
439443

440444
@doc """
@@ -452,7 +456,7 @@ defmodule QuickBEAM do
452456
if resolve_mode(runtime, opts) == :beam do
453457
load_module_beam(runtime, name, code)
454458
else
455-
QuickBEAM.Runtime.load_module(runtime, name, code)
459+
Runtime.load_module(runtime, name, code)
456460
end
457461
end
458462

@@ -476,7 +480,7 @@ defmodule QuickBEAM do
476480
"""
477481
@spec load_addon(runtime(), String.t(), keyword()) :: {:ok, term()} | {:error, term()}
478482
def load_addon(runtime, path, opts \\ []) do
479-
QuickBEAM.Runtime.load_addon(runtime, path, opts)
483+
Runtime.load_addon(runtime, path, opts)
480484
end
481485

482486
@doc """
@@ -493,13 +497,13 @@ defmodule QuickBEAM do
493497
"""
494498
@spec reset(runtime()) :: :ok | {:error, String.t()}
495499
def reset(runtime) do
496-
QuickBEAM.Runtime.reset(runtime)
500+
Runtime.reset(runtime)
497501
end
498502

499503
@doc "Stop a runtime and free its resources."
500504
@spec stop(runtime()) :: :ok
501505
def stop(runtime) do
502-
QuickBEAM.Runtime.stop(runtime)
506+
Runtime.stop(runtime)
503507
end
504508

505509
@doc """
@@ -537,7 +541,7 @@ defmodule QuickBEAM do
537541
@doc "Return QuickJS memory usage statistics."
538542
@spec memory_usage(runtime()) :: map()
539543
def memory_usage(runtime) do
540-
QuickBEAM.Runtime.memory_usage(runtime)
544+
Runtime.memory_usage(runtime)
541545
end
542546

543547
@doc """
@@ -548,7 +552,7 @@ defmodule QuickBEAM do
548552
"""
549553
@spec send_message(runtime(), term()) :: :ok
550554
def send_message(runtime, message) do
551-
QuickBEAM.Runtime.send_message(runtime, message)
555+
Runtime.send_message(runtime, message)
552556
end
553557

554558
@doc """
@@ -596,7 +600,7 @@ defmodule QuickBEAM do
596600
@spec get_global(runtime(), String.t()) :: js_result()
597601
def get_global(runtime, name, opts \\ []) when is_binary(name) do
598602
if resolve_mode(runtime, opts) == :beam do
599-
persistent = QuickBEAM.BeamVM.Heap.get_persistent_globals()
603+
persistent = Heap.get_persistent_globals()
600604
raw = Map.get(persistent, name, :undefined)
601605
{:ok, convert_beam_value(raw)}
602606
else
@@ -620,9 +624,9 @@ defmodule QuickBEAM do
620624
@spec set_global(runtime(), String.t(), term()) :: :ok
621625
def set_global(runtime, name, value, opts \\ []) when is_binary(name) do
622626
if resolve_mode(runtime, opts) == :beam do
623-
persistent = QuickBEAM.BeamVM.Heap.get_persistent_globals()
627+
persistent = Heap.get_persistent_globals()
624628
js_val = elixir_to_js(value)
625-
QuickBEAM.BeamVM.Heap.put_persistent_globals(Map.put(persistent, name, js_val))
629+
Heap.put_persistent_globals(Map.put(persistent, name, js_val))
626630
:ok
627631
else
628632
GenServer.call(runtime, {:set_global, name, value}, :infinity)
@@ -658,7 +662,7 @@ defmodule QuickBEAM do
658662
"""
659663
@spec dom_find(runtime(), String.t()) :: {:ok, tuple() | nil}
660664
def dom_find(runtime, selector) do
661-
QuickBEAM.Runtime.dom_find(runtime, selector)
665+
Runtime.dom_find(runtime, selector)
662666
end
663667

664668
@doc """
@@ -673,7 +677,7 @@ defmodule QuickBEAM do
673677
"""
674678
@spec dom_find_all(runtime(), String.t()) :: {:ok, list()}
675679
def dom_find_all(runtime, selector) do
676-
QuickBEAM.Runtime.dom_find_all(runtime, selector)
680+
Runtime.dom_find_all(runtime, selector)
677681
end
678682

679683
@doc """
@@ -685,7 +689,7 @@ defmodule QuickBEAM do
685689
"""
686690
@spec dom_text(runtime(), String.t()) :: {:ok, String.t()}
687691
def dom_text(runtime, selector) do
688-
QuickBEAM.Runtime.dom_text(runtime, selector)
692+
Runtime.dom_text(runtime, selector)
689693
end
690694

691695
@doc """
@@ -699,7 +703,7 @@ defmodule QuickBEAM do
699703
"""
700704
@spec dom_attr(runtime(), String.t(), String.t()) :: {:ok, String.t() | nil}
701705
def dom_attr(runtime, selector, attr_name) do
702-
QuickBEAM.Runtime.dom_attr(runtime, selector, attr_name)
706+
Runtime.dom_attr(runtime, selector, attr_name)
703707
end
704708

705709
@doc """
@@ -711,6 +715,6 @@ defmodule QuickBEAM do
711715
"""
712716
@spec dom_html(runtime()) :: {:ok, String.t()}
713717
def dom_html(runtime) do
714-
QuickBEAM.Runtime.dom_html(runtime)
718+
Runtime.dom_html(runtime)
715719
end
716720
end

lib/quickbeam/beam_vm/builtin.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ defmodule QuickBEAM.BeamVM.Builtin do
143143

144144
# ── Runtime dispatch ──
145145

146-
alias QuickBEAM.BeamVM.{Heap, Bytecode}
146+
alias QuickBEAM.BeamVM.{Bytecode, Heap}
147147

148148
def call({:builtin, _, cb}, args, this), do: cb.(args, this)
149149

lib/quickbeam/beam_vm/bytecode.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ defmodule QuickBEAM.BeamVM.Bytecode do
292292

293293
# ── Function bytecode ──
294294
# Matches JS_ReadFunctionTag exactly.
295-
#
295+
#
296296
# Layout:
297297
# flags (u16 raw LE)
298298
# is_strict_mode (u8)

0 commit comments

Comments
 (0)