Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ const outputBlob = await idbDevice.readFileAsBlob("/filename");

## DataDevice

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

### Usage

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

```javascript
const dataDevice = await CheerpX.DataDevice.create();
Expand All @@ -202,7 +202,7 @@ const cx = await CheerpX.Linux.create({

### Adding files

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

```javascript
await dataDevice.writeFile("/filename", "File content here");
Expand Down
5 changes: 1 addition & 4 deletions sites/cheerpx/src/content/docs/11-guides/input-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,11 @@ For more on `IDBDevice` operations, see the [CheerpX IDBDevice].

## Accessing JS Data in the Filesystem via DataDevice

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.

For more information, see the [CheerpX DataDevice].
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.

[CheerpX documentations]: https://cheerpx.io/docs/overview
[CheerpX console]: https://cheerpx.io/docs/reference/CheerpX-Linux-setConsole
[CheerpX Custom console]: https://cheerpx.io/docs/reference/CheerpX-Linux-setCustomConsole
[CheerpX DataDevice]: https://cheerpx.io/docs/guides/File-System-support#datadevice
[CheerpX IDBDevice]: https://cheerpx.io/docs/guides/File-System-support#idbdevice
[Frequently Asked Questions]: https://cheerpx.io/docs/faq
[xterm.js]: https://xtermjs.org/
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
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".

[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
2 changes: 1 addition & 1 deletion sites/cheerpx/src/content/docs/20-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Yes, `WebDevice` can handle third-party origins as paths, but it's important to

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

`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` (IndexedDB) or Ext2.
[`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` (IndexedDB) or Ext2.

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

Expand Down