-
Notifications
You must be signed in to change notification settings - Fork 19
Added DataDevice reference #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
sites/cheerpx/src/content/docs/12-reference/CheerpX.DataDevice/create.md
AtibQur marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --- | ||
| title: create | ||
| description: Create a CheerpX DataDevice instance to store temporary data in memory. | ||
| --- | ||
|
|
||
| ```ts | ||
| namespace CheerpX { | ||
| class DataDevice { | ||
| static async create(): Promise<DataDevice>; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| - None | ||
|
|
||
| ## Returns | ||
|
|
||
| `CheerpX.DataDevice.create` returns a [Promise] that resolves to a `DataDevice` instance. You can use this instance to create a temporary in-memory filesystem in your CheerpX environment. | ||
|
|
||
| ## Example | ||
|
|
||
| Create a `DataDevice` instance for in-memory storage. | ||
|
|
||
| ```ts {7, 13} | ||
| // Create a read-only block device for a disk image stored on the HTTP server | ||
| const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2"); | ||
| // Make the block device read-write using a persistent IndexedDB overlay | ||
| const idbDevice = await CheerpX.IDBDevice.create("block_idbDevice"); | ||
| const overlayDevice = await CheerpX.OverlayDevice.create( | ||
| blockDevice, | ||
| idbDevice | ||
| ); | ||
| // Add a device to expose JavaScript data in the VM | ||
| const dataDevice = await CheerpX.DataDevice.create(); | ||
| // Initialize the CheerpX environment | ||
| const mountPoints = [ | ||
| // Use the disk image as the filesystem root | ||
| { type: "ext2", path: "/", dev: overlayDevice }, | ||
| // Add the DataDevice to a known location | ||
| { type: "dir", path: "/data", dev: dataDevice }, | ||
| ]; | ||
| const cx = await CheerpX.Linux.create({ | ||
| mounts: mountPoints, | ||
| }); | ||
| ``` | ||
|
|
||
| In this example, the `DataDevice` is created using `CheerpX.DataDevice.create()` and mounted at `/data` in the Linux environment inside the CheerpX system. | ||
|
|
||
| [Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise |
91 changes: 91 additions & 0 deletions
91
sites/cheerpx/src/content/docs/12-reference/CheerpX.DataDevice/writeFIle.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| --- | ||
| title: writeFile | ||
| description: Write data to a new file in the CheerpX DataDevice. | ||
| --- | ||
|
|
||
| ```ts | ||
| namespace CheerpX { | ||
| class DataDevice { | ||
| async writeFile( | ||
| filename: string, | ||
| contents: string | Uint8Array | ||
| ): Promise<void>; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| - **filename (`string`)** - The path to the file within the device, starting with a `/` (e.g., `/filename`). The path should not include the mount point. | ||
| - **contents (`string | Uint8Array`)** - The data to write to the file. Can be either a string or a `Uint8Array`. | ||
|
|
||
| ## Returns | ||
|
|
||
| `CheerpX.DataDevice.writeFile` returns a [Promise] that resolves when the file has been created and written to, but it does not return any value. | ||
|
|
||
| ## Example | ||
|
|
||
| Write a file to the `DataDevice`. | ||
|
|
||
| ```ts {29} | ||
| // Create a read-only block device for a disk image stored on the HTTP server | ||
| const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2"); | ||
| // Make the block device read-write using a persistent IndexedDB overlay | ||
| const idbDevice = await CheerpX.IDBDevice.create("block_cpp"); | ||
| const overlayDevice = await CheerpX.OverlayDevice.create( | ||
| blockDevice, | ||
| idbDevice | ||
| ); | ||
| // Add a device to expose JavaScript data in the VM | ||
| const dataDevice = await CheerpX.DataDevice.create(); | ||
| // Initialize the CheerpX environment | ||
| const mountPoints = [ | ||
| // Use the disk image as the filesystem root | ||
| { type: "ext2", path: "/", dev: overlayDevice }, | ||
| // Add the DataDevice to a known location | ||
| { type: "dir", path: "/data", dev: dataDevice }, | ||
| // Add the required Linux pseudo-filesystem | ||
| { type: "devs", path: "/dev" }, | ||
| { type: "devpts", path: "/dev/pts" }, | ||
| { type: "proc", path: "/proc" }, | ||
| ]; | ||
| const cx = await CheerpX.Linux.create({ | ||
| mounts: mountPoints, | ||
| }); | ||
|
|
||
| // Setup the text console | ||
| cx.setConsole(document.getElementById("console")); | ||
|
|
||
| async function compileAndRun(cx, dataDevice, srcCode, inputName, outputName) { | ||
| // Make the source code available as a file | ||
| await dataDevice.writeFile("/" + inputName, srcCode); | ||
| // Compile the source code by calling g++ | ||
| await cx.run( | ||
| "/usr/bin/g++", | ||
| ["-v", "/data/" + inputName, "-o", "/" + outputName], | ||
| { | ||
| env: ["PATH=/usr/bin"], | ||
| } | ||
| ); | ||
| await cx.run("/" + outputName, []); | ||
| } | ||
|
|
||
| const srcCode = ` | ||
| #include <iostream> | ||
|
|
||
| int main() | ||
| { | ||
| std::cout << "Hello World" << std::endl; | ||
| return 0; | ||
| } | ||
| `; | ||
| await compileAndRun(cx, dataDevice, srcCode, "hello.cpp", "hello"); | ||
| ``` | ||
|
|
||
| In this example: | ||
|
|
||
| The `writeFile` method is used to write source code (`srcCode`) into a file (`hello.cpp`) on the `DataDevice`. | ||
|
|
||
| After that, the CheerpX environment runs a `g++` command to compile the source code, and then executes the compiled file to output "Hello World". | ||
AtibQur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.