Skip to content

Commit 5994480

Browse files
authored
Added DataDevice reference (#212)
1 parent e836ffb commit 5994480

File tree

5 files changed

+159
-36
lines changed

5 files changed

+159
-36
lines changed

sites/cheerpx/src/content/docs/11-guides/File-System-support.md

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ For more details on reading files using [`IDBDevice`](/docs/reference/CheerpX.ID
161161

162162
## DataDevice
163163

164-
`DataDevice` is an in-memory filesystem useful for temporary data storage or passing data from JavaScript to the CheerpX environment.
164+
[`DataDevice`](/docs/reference/CheerpX.DataDevice) is an in-memory filesystem useful for temporary data storage or passing data from JavaScript to the CheerpX environment.
165165

166166
### Usage
167167

168-
Create a DataDevice using the `CheerpX.DataDevice.create()` method:
168+
[`Create`](/docs/reference/CheerpX.DataDevice/create) a [`DataDevice`](/docs/reference/CheerpX.DataDevice) using the `CheerpX.DataDevice.create()` method:
169169

170170
```javascript
171171
const dataDevice = await CheerpX.DataDevice.create();
@@ -177,41 +177,12 @@ const cx = await CheerpX.Linux.create({
177177

178178
### Adding files
179179

180-
You can add files to a DataDevice from JavaScript using the `writeFile` method:
180+
You can add files to a [`DataDevice`](/docs/reference/CheerpX.DataDevice) from JavaScript using the [`writeFile`](/docs/reference/CheerpX.DataDevice/writeFIle) method:
181181

182182
```javascript
183183
await dataDevice.writeFile("/filename", "File content here");
184184
```
185185

186-
### `dataDevice.writeFile`
187-
188-
`CheerpX.DataDevice` provides a method to write data to new files within the mounted device. This utility is limited to creating files at the root level of the device.
189-
190-
```js
191-
await dataDevice.writeFile(filename: string, contents: string | Uint8Array): Promise<void>
192-
```
193-
194-
**Parameters**:
195-
196-
- **filename**: A string representing the path to the file within the device, starting with a `/` (e.g., "/filename"). Do not include the mount point.
197-
- **contents**: The data to write to the file. Can be either a string or a Uint8Array.
198-
199-
**Returns**:
200-
201-
The method returns a Promise that resolves when the file has been created and written to. It doesn't return any value.
202-
203-
Example:
204-
205-
```js
206-
const dataDevice = await CheerpX.DataDevice.create();
207-
await dataDevice.writeFile("/filename", "contents");
208-
```
209-
210-
> [!note] Note
211-
>
212-
> - This is the only way to create files in this device.
213-
> - Modifying existing files or creating files in subdirectories is not possible.
214-
215186
## Block devices with ext2
216187

217188
CheerpX supports ext2 filesystems, which can be configured as an overlay device. This allows for a flexible setup that can combine different storage types.

sites/cheerpx/src/content/docs/11-guides/input-output.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ console.log(await outputBlob.text());
8888
8989
## Accessing JS Data in the Filesystem via DataDevice
9090

91-
The `DataDevice` API exposes JavaScript data via the filesystem. This device provides read-only access to a `Uint8Array` and a JavaScript `String`. It is particularly useful for transferring data from JavaScript to programs running in CheerpX.
92-
93-
For more information, see the [CheerpX DataDevice].
91+
The [`DataDevice`](/docs/reference/CheerpX.DataDevice) API exposes JavaScript data via the filesystem. This device provides read-only access to a `Uint8Array` and a JavaScript `String`. It is particularly useful for transferring data from JavaScript to programs running in CheerpX.
9492

9593
[CheerpX documentations]: https://cheerpx.io/docs/overview
9694
[CheerpX DataDevice]: https://cheerpx.io/docs/guides/File-System-support#datadevice
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: create
3+
description: Create a CheerpX DataDevice instance to store temporary data in memory.
4+
---
5+
6+
```ts
7+
namespace CheerpX {
8+
class DataDevice {
9+
static async create(): Promise<DataDevice>;
10+
}
11+
}
12+
```
13+
14+
## Parameters
15+
16+
- None
17+
18+
## Returns
19+
20+
`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.
21+
22+
## Example
23+
24+
Create a `DataDevice` instance for in-memory storage.
25+
26+
```ts {7, 13}
27+
// Create a read-only block device for a disk image stored on the HTTP server
28+
const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2");
29+
// Make the block device read-write using a persistent IndexedDB overlay
30+
const idbDevice = await CheerpX.IDBDevice.create("block_idbDevice");
31+
const overlayDevice = await CheerpX.OverlayDevice.create(
32+
blockDevice,
33+
idbDevice
34+
);
35+
// Add a device to expose JavaScript data in the VM
36+
const dataDevice = await CheerpX.DataDevice.create();
37+
// Initialize the CheerpX environment
38+
const mountPoints = [
39+
// Use the disk image as the filesystem root
40+
{ type: "ext2", path: "/", dev: overlayDevice },
41+
// Add the DataDevice to a known location
42+
{ type: "dir", path: "/data", dev: dataDevice },
43+
];
44+
const cx = await CheerpX.Linux.create({
45+
mounts: mountPoints,
46+
});
47+
```
48+
49+
In this example, the `DataDevice` is created using `CheerpX.DataDevice.create()` and mounted at `/data` in the Linux environment inside the CheerpX system.
50+
51+
If you’d like to learn more about related topics, check out these guides:
52+
53+
- [Files and filesystems](/docs/guides/File-System-support) – Managing files and storage in CheerpX.
54+
- [Custom disk images](/docs/guides/custom-images) – Creating and using custom disk images.
55+
- [Input and output](/docs/guides/input-output) – Handling data flow in your environment.
56+
57+
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: writeFile
3+
description: Write data to a new file in the CheerpX DataDevice.
4+
---
5+
6+
```ts
7+
namespace CheerpX {
8+
class DataDevice {
9+
async writeFile(
10+
filename: string,
11+
contents: string | Uint8Array
12+
): Promise<void>;
13+
}
14+
}
15+
```
16+
17+
## Parameters
18+
19+
- **filename (`string`)** - The path to the file within the device, starting with a `/` (e.g., `/filename`). The path should not include the mount point.
20+
- **contents (`string | Uint8Array`)** - The data to write to the file. Can be either a string or a `Uint8Array`.
21+
22+
## Returns
23+
24+
`CheerpX.DataDevice.writeFile` returns a [Promise] that resolves when the file has been created and written to, but it does not return any value.
25+
26+
## Example
27+
28+
Write a file to the `DataDevice`.
29+
30+
```ts {29}
31+
// Create a read-only block device for a disk image stored on the HTTP server
32+
const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2");
33+
// Make the block device read-write using a persistent IndexedDB overlay
34+
const idbDevice = await CheerpX.IDBDevice.create("block_cpp");
35+
const overlayDevice = await CheerpX.OverlayDevice.create(
36+
blockDevice,
37+
idbDevice
38+
);
39+
// Add a device to expose JavaScript data in the VM
40+
const dataDevice = await CheerpX.DataDevice.create();
41+
// Initialize the CheerpX environment
42+
const mountPoints = [
43+
// Use the disk image as the filesystem root
44+
{ type: "ext2", path: "/", dev: overlayDevice },
45+
// Add the DataDevice to a known location
46+
{ type: "dir", path: "/data", dev: dataDevice },
47+
// Add the required Linux pseudo-filesystem
48+
{ type: "devs", path: "/dev" },
49+
{ type: "devpts", path: "/dev/pts" },
50+
{ type: "proc", path: "/proc" },
51+
];
52+
const cx = await CheerpX.Linux.create({
53+
mounts: mountPoints,
54+
});
55+
56+
// Setup the text console
57+
cx.setConsole(document.getElementById("console"));
58+
59+
async function compileAndRun(cx, dataDevice, srcCode, inputName, outputName) {
60+
// Make the source code available as a file
61+
await dataDevice.writeFile("/" + inputName, srcCode);
62+
// Compile the source code by calling g++
63+
await cx.run(
64+
"/usr/bin/g++",
65+
["-v", "/data/" + inputName, "-o", "/" + outputName],
66+
{
67+
env: ["PATH=/usr/bin"],
68+
}
69+
);
70+
await cx.run("/" + outputName, []);
71+
}
72+
73+
const srcCode = `
74+
#include <iostream>
75+
76+
int main()
77+
{
78+
std::cout << "Hello World" << std::endl;
79+
return 0;
80+
}
81+
`;
82+
await compileAndRun(cx, dataDevice, srcCode, "hello.cpp", "hello");
83+
```
84+
85+
In this example:
86+
87+
The `writeFile` method is used to write source code (`srcCode`) into a file (`hello.cpp`) on the `DataDevice`.
88+
89+
After that, the CheerpX environment runs a `g++` command to compile the source code, and then executes the compiled file to output **Hello World**.
90+
91+
If you’d like to learn more about related topics, check out these guides:
92+
93+
- [Files and filesystems](/docs/guides/File-System-support) – Managing files and storage in CheerpX.
94+
- [Custom disk images](/docs/guides/custom-images) – Creating and using custom disk images.
95+
- [Input and output](/docs/guides/input-output) – Handling data flow in your environment.
96+
97+
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

sites/cheerpx/src/content/docs/20-faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Yes, `WebDevice` can handle third-party origins as paths, but it's important to
2222

2323
## Why can't I execute files directly from a `DataDevice`?
2424

25-
`DataDevice` in CheerpX does not have full support for Linux mode bits, and in particular it lacks the _**executable**_ bit. This means you can read data from it, but you cannot execute files directly from it. To execute files that are in a `DataDevice`, you need to first copy the files to a filesystem with complete support for mode bits, such as [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) (IndexedDB) or Ext2.
25+
[`DataDevice`](/docs/reference/CheerpX.DataDevice) in CheerpX does not have full support for Linux mode bits, and in particular it lacks the _**executable**_ bit. This means you can read data from it, but you cannot execute files directly from it. To execute files that are in a [`DataDevice`](/docs/reference/CheerpX.DataDevice), you need to first copy the files to a filesystem with complete support for mode bits, such as [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) (IndexedDB) or Ext2.
2626

2727
## Why can't CheerpX do what v86 does in terms of disk access and networking?
2828

0 commit comments

Comments
 (0)