Commit graph

21891 commits

Author SHA1 Message Date
Gunnar Beutner
93eae063a1 LibJS: Make sure that if expressions yield the correct value
When evaluated as an expression "if (true) { 3 } else { 5 }"
should yield 3. This updates the bytecode interpreter to make
it so.
2021-06-07 22:10:57 +02:00
Gunnar Beutner
2c10bd72f2 LibJS: Make sure scope expressions yield the correct value
When evaluated as an expression "{ 3 }" should yield 3. This updates
the bytecode interpreter to make it so.
2021-06-07 22:10:57 +02:00
Nick Miller
ed5777eb0a LibAudio: WavLoader: Avoid reading partial samples
When samples are requested in `Audio::Loader::get_more_samples`,
the request comes in as a max number of bytes to read.

However, the requested number of bytes may not be an even multiple
of the bytes per sample of the loaded file. If this is the case, and
the bytes are read from the file/stream, then
the last sample will be a partial/runt sample, which then offsets
the remainder of the stream, causing white noise in playback.

This bug was discovered when trying to play 24-bit Wave files, which
happened to have a sample size that never aligned with the number
of requested bytes.

This commit fixes the bug by only reading a multiple of
"bytes per sample" for the loaded file.
2021-06-08 00:38:54 +04:30
Nick Miller
3938b56577 LibAudio+LibCore: Remove unnecessary IODeviceStreamReader.h
IODeviceStreamReader isn't pulling its weight.
It's essentially a subset of InputFileStream with only one user
(WavLoader).

This refactors WavLoader to use InputFileStream instead.
2021-06-08 00:38:54 +04:30
Brian Gianforcaro
06ea31d0d5 CatDog: Enhance the speech bubble artificial intelligence
Enable cat dog to greet you, and help you with yak shave sessions.
2021-06-07 21:52:16 +02:00
Nick Miller
c3a60a5dcd LibGUI+SoundPlayer: Add Slider option to jump to cursor
When the cursor is clicked outside of the slider knob,
the current behavior is that it will step up or down by the
Slider page step amount.

This commit adds an option to jump the slider knob
directly to the where the mouse cursor is on mouse down events.

This behavior is disabled by default. It must be enabled with
`Slider::set_jump_to_cursor()`.

Jump to cursor is enabled in SoundPlayer since most music players
have this behavior.
2021-06-08 00:10:54 +04:30
Ryan Chandler
6612e026ba LibJS: Add <<, >> and >>> assignment operators 2021-06-07 21:23:11 +02:00
Luke
1e10965e61 LibJS: Add bytecode ops for <<, >> and >>> 2021-06-07 21:18:34 +02:00
Ryan Chandler
6681415f58 LibJS: Add support for various assignment operators 2021-06-07 21:18:19 +02:00
Ryan Chandler
18ac7fde12 LibJS: Add support for typed equality checks 2021-06-07 21:17:29 +02:00
Gunnar Beutner
9e69ffc1b1 LibJS: Add bytecode generation for EmptyStatement 2021-06-07 20:06:46 +01:00
Linus Groh
2b8a2542a0 LibJS: Add bytecode generation for DebuggerStatement
No-op. :^)
2021-06-07 20:05:50 +01:00
Linus Groh
fa9bad912e LibJS: Add bytecode instructions for a bunch of unary operators
~, !, +, -, typeof, and void.
2021-06-07 19:53:47 +01:00
Linus Groh
54fc7079c6 LibJS: Remove redundant Value() from bytecode bitwise ops execute() 2021-06-07 19:53:22 +01:00
Gunnar Beutner
8ed5b7dcfa LibJS: Add bytecode ops for loading boolean and null values 2021-06-07 20:26:45 +02:00
Ryan Chandler
6e2b266534 LibJS: Fix AbstractInequals returning result of AbstractEquals 2021-06-07 20:25:56 +02:00
Luke
ae763f1ade LibJS: Add bytecode ops for &, | and ^ 2021-06-07 20:17:24 +02:00
Gunnar Beutner
4be3374b24 LibJS: Add bytecode ops for >, >= and <= 2021-06-07 20:09:23 +02:00
Nick Miller
73b8acf432 CONTRIBUTING.md: Add Pull Request Q&A section
This is an attempt to clarify general pull request etiquette.
Answers are copied verbatim from `@awesomekling`'s responses in Discord.
2021-06-07 20:08:35 +02:00
Gunnar Beutner
55f0791b13 LibJS: Add bytecode instructions for modulo and exponentiation 2021-06-07 19:40:27 +02:00
Jelle Raaijmakers
6af9d87258 Toolchain: Add ccache to Dockerfile
Following up on 2d38d56e, we were missing this in our Dockerfile.
2021-06-07 19:22:03 +02:00
Gunnar Beutner
3c5ce9b5b7 LibJS: Add bytecode instructions for multiplication and division 2021-06-07 19:21:36 +02:00
Jelle Raaijmakers
5183952d1d Ports: Add Beneath a Steel Sky 2021-06-07 19:04:57 +02:00
Andreas Kling
4776c50d5f LibJS: Remove unused Bytecode::Block::m_buffer_end 2021-06-07 18:30:39 +02:00
Andreas Kling
312297ac38 LibJS: Add placeholder bytecode block sealing mechanism
After compiling bytecode, we should mark the memory read-only.
This currently does not work because it breaks instruction destruction.

I'm adding this anyway with a FIXME so we don't forget about it. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
4ba2eb8fe5 LibJS: Cache generated bytecode for ScriptFunction
It's silly to generate new bytecode every time you call a function.
Let's just cache the code instead. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
7cbe4daa7c LibJS: Move bytecode debug spam behind JS_BYTECODE_DEBUG :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
e7d69c5d3c LibJS: Devirtualize and pack the bytecode stream :^)
This patch changes the LibJS bytecode to be a stream of instructions
packed one-after-the-other in contiguous memory, instead of a vector
of OwnPtr<Instruction>. This should be a lot more cache-friendly. :^)

Instructions are also devirtualized and instead have a type field
using a new Instruction::Type enum.

To iterate over a bytecode stream, one must now use
Bytecode::InstructionStreamIterator.
2021-06-07 18:11:59 +02:00
Andreas Kling
845f2826aa LibJS: Reset Bytecode::Interpreter's m_return_value when leaving run()
Otherwise it will cause complete unwind since all parent run() loops
will see the same m_return_value being non-empty and break out.
2021-06-07 18:11:59 +02:00
Andreas Kling
9330163b0b LibJS: Make sure the global CallFrame doesn't go out of scope
The Bytecode::Interpreter will push a global call frame if needed,
and it needs to make sure that call frame survives until the end
of the Interpreter::run() function.
2021-06-07 18:11:59 +02:00
Andreas Kling
0cc9d47e1b LibJS: Add AbstractEquals bytecode instruction for == comparison :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
4bdfe73895 LibJS: Add basic support for "continue" in the bytecode VM
Unlike the convoluted unwind-until-scope-type mechanism in the AST
interpreter, "continue" maps to a simple Bytecode::Op::Jump here. :^)

We know where to jump based on a stack of "continuable scopes" that
we now maintain on the Bytecode::Generator as we go.

Note that this only supports bare "continue", not continue-with-label.
2021-06-07 18:11:59 +02:00
Andreas Kling
79eac08f5b LibJS: Add basic "if" statement support to the bytecode VM :^)
This also required making Bytecode::Op::Jump support lazy linking
to a target label.

I left a FIXME here about having the "if" statement return the result
value from the taken branch statement. That's what the AST interpreter
does but I'm not sure if it's actually required.
2021-06-07 18:11:59 +02:00
Andreas Kling
80b1604b0a LibJS: Compile ScriptFunctions into bytecode and run them that way :^)
If there's a current Bytecode::Interpreter in action, ScriptFunction
will now compile itself into bytecode and execute in that context.

This patch also adds the Return bytecode instruction so that we can
actually return values from called functions. :^)

Return values are propagated from callee to caller via the caller's
$0 register. Bytecode::Interpreter now keeps a stack of register
"windows". These are not very efficient, but it should be pretty
straightforward to convert them to e.g a sliding register window
architecture later on.

This is pretty dang cool! :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
b609fc6d51 js: Exit the program after dumping and/or running bytecode
Otherwise we'd run the same program again in the AST interpreter.
2021-06-07 18:11:59 +02:00
Andreas Kling
dc63958478 LibJS: Support basic function calls in the bytecode world :^)
This patch adds the Call bytecode instruction which is emitted for the
CallExpression AST node.

It's pretty barebones and doesn't handle 'this' values properly, etc.
But it can perform basic function calls! :^)

Note that the called function will *not* execute as bytecode, but will
simply fall back into the old codepath and use the AST interpreter.
2021-06-07 18:11:59 +02:00
Andreas Kling
1eafaf67fe LibJS: Add a new EnterScope bytecode instruction
This is intended to perform the same duties as enter_scope() does in
the AST tree-walk interpreter:

- Hoisted function declaration processing
- Hoisted variable declaration processing
- ... maybe more

This first cut only implements the function declaration processing.
2021-06-07 18:11:59 +02:00
Andreas Kling
2316a084bf LibJS: Create a global/outermost CallFrame for Bytecode::Interpreter
Note that we don't yet support nested calls using bytecode.
2021-06-07 18:11:59 +02:00
Andreas Kling
32561bb90d LibJS: Add GetById bytecode instruction for object property retrieval
Same as PutById but in the other direction. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
14cfc44855 LibJS: Add PutById bytecode instruction for object property assignment
Note that this is only used for non-computed accesses. Computed access
is not yet implemented. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
bea6e31ddc LibJS: Add a NewObject bytecode instruction for ObjectExpression :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
f2863b5a89 LibJS: Generate bytecode for do...while statements :^)
This was quite straightforward using the same label/jump machinery that
we added for while statements.

The main addition here is a new JumpIfTrue bytecode instruction.
2021-06-07 18:11:59 +02:00
Andreas Kling
bd1a5e282a LibJS: Add AbstractInequals bytecode instruction :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
6ae9346cd3 LibJS: Add basic support for while loops in the bytecode engine
This introduces two new instructions: Jump and JumpIfFalse.
Jumps are made to a Bytecode::Label, which is a simple object that
represents a location in the bytecode stream.

Note that you may not always know the target of a jump when adding the
jump instruction itself, but we can just update the instruction later
on during codegen once we know where the jump target is.

The Bytecode::Interpreter now implements jumping via a jump slot that
gets checked after each instruction to see if a jump is pending.
If not, we just increment the PC as usual.
2021-06-07 18:11:59 +02:00
Andreas Kling
91640d0727 LibJS: Add LessThan bytecode instruction :^) 2021-06-07 18:11:59 +02:00
Andreas Kling
6d66cdc668 LibJS: Print bytecode registers with format "$num" instead of "rnum" 2021-06-07 18:11:59 +02:00
Andreas Kling
4934d16397 LibJS: Make Bytecode::Generator::emit() return the created instruction
This will be useful for instructions that need to be modified later on
during code generation, e.g jumps. :^)
2021-06-07 18:11:59 +02:00
Andreas Kling
0553e0b048 LibJS: Move AST bytecode generation virtuals to separate cpp file
This will hopefully make it a bit more pleasant to edit this, as things
will just get larger and larger.
2021-06-07 18:11:59 +02:00
Andreas Kling
2b9fbd10ed LibJS: Add Sub bytecode instruction (subtract values) 2021-06-07 18:11:59 +02:00
Andreas Kling
23a4448862 LibJS: Add formatting helper for Bytecode::Register 2021-06-07 18:11:59 +02:00