diff --git a/apps/script_console.cpp b/apps/script_console.cpp index 64e65c9..47858c0 100644 --- a/apps/script_console.cpp +++ b/apps/script_console.cpp @@ -329,6 +329,37 @@ int External_DirectoryInternalNextIteration(ExecutionContext *context, Value *re return 3; } +int ExternalOpenDocumentEnumerate(ExecutionContext *context, Value *returnValue) { + EsBuffer buffer = { .canGrow = true }; + _EsOpenDocumentEnumerate(&buffer); + + size_t count = 0; + EsBufferReadInto(&buffer, &count, sizeof(size_t)); + + uintptr_t index = HeapAllocate(context); + context->heap[index].type = T_LIST; + context->heap[index].internalValuesAreManaged = true; + context->heap[index].length = count; + context->heap[index].list = (Value *) EsHeapAllocate(sizeof(Value) * count, false); + + for (uintptr_t i = 0; i < count; i++) { + size_t pathBytes = 0; + EsBufferReadInto(&buffer, &pathBytes, sizeof(size_t)); + char *path = (char *) EsHeapAllocate(pathBytes, false); + EsBufferReadInto(&buffer, path, pathBytes); + context->heap[index].list[i].i = HeapAllocate(context); + context->heap[context->heap[index].list[i].i].type = T_STR; + context->heap[context->heap[index].list[i].i].bytes = pathBytes; + context->heap[context->heap[index].list[i].i].text = path; + } + + EsHeapFree(buffer.out); + EsAssert(!buffer.error); + returnValue->i = index; + + return 3; +} + #define EXTERNAL_STUB(name) int name(ExecutionContext *, Value *) { EsPrint("Unimplemented " #name "\n"); EsAssert(false); return -1; } // TODO What to do with these? diff --git a/desktop/files.cpp b/desktop/files.cpp index 9dcb5ef..700c666 100644 --- a/desktop/files.cpp +++ b/desktop/files.cpp @@ -295,8 +295,8 @@ EsError MountPointAdd(const char *prefix, size_t prefixBytes, EsHandle base, boo return error; } -EsError EsMountPointAdd(const char *prefix, size_t prefixBytes, EsHandle base) { - return MountPointAdd(prefix, prefixBytes, base, true); +EsError EsMountPointAdd(const char *prefix, ptrdiff_t prefixBytes, EsHandle base) { + return MountPointAdd(prefix, prefixBytes == -1 ? EsCStringLength(prefix) : prefixBytes, base, true); } bool NodeFindMountPoint(const char *prefix, size_t prefixBytes, EsMountPoint *result, bool mutexTaken) { @@ -321,14 +321,19 @@ bool NodeFindMountPoint(const char *prefix, size_t prefixBytes, EsMountPoint *re return found; } -bool EsMountPointRemove(const char *prefix, size_t prefixBytes) { +bool EsMountPointRemove(const char *prefix, ptrdiff_t prefixBytes) { + if (prefixBytes == -1) { + prefixBytes = EsCStringLength(prefix); + } + EsMutexAcquire(&api.mountPointsMutex); bool found = false; for (uintptr_t i = 0; i < api.mountPoints.Length(); i++) { EsMountPoint *mountPoint = &api.mountPoints[i]; - if (prefixBytes >= mountPoint->prefixBytes && 0 == EsMemoryCompare(prefix, mountPoint->prefix, mountPoint->prefixBytes)) { + if ((uintptr_t) prefixBytes >= mountPoint->prefixBytes + && 0 == EsMemoryCompare(prefix, mountPoint->prefix, mountPoint->prefixBytes)) { EsAssert(mountPoint->addedByApplication); EsHandleClose(mountPoint->base); api.mountPoints.Delete(i); @@ -341,7 +346,11 @@ bool EsMountPointRemove(const char *prefix, size_t prefixBytes) { return found; } -bool EsMountPointGetVolumeInformation(const char *prefix, size_t prefixBytes, EsVolumeInformation *information) { +bool EsMountPointGetVolumeInformation(const char *prefix, ptrdiff_t prefixBytes, EsVolumeInformation *information) { + if (prefixBytes == -1) { + prefixBytes = EsCStringLength(prefix); + } + EsMutexAcquire(&api.mountPointsMutex); EsMountPoint mountPoint; bool found = NodeFindMountPoint(prefix, prefixBytes, &mountPoint, true); diff --git a/desktop/os.header b/desktop/os.header index cd76c48..ba848e3 100644 --- a/desktop/os.header +++ b/desktop/os.header @@ -2056,9 +2056,9 @@ function EsFileOffsetDifference EsFileStoreGetSize(EsFileStore *file); // Return function void *EsFileStoreMap(EsFileStore *file, size_t *fileSize, uint32_t flags); // These calls require permission_all_files. -function bool EsMountPointGetVolumeInformation(const char *prefix, size_t prefixBytes, EsVolumeInformation *information); // Returns false if the mount point does not exist. -function EsError EsMountPointAdd(const char *prefix, size_t prefixBytes, EsHandle base); // The system maintains a duplicate of the base handle. -function bool EsMountPointRemove(const char *prefix, size_t prefixBytes); // Returns false if the mount point does not exist. You can only remove mount points you added with EsMountPointAdd. +function bool EsMountPointGetVolumeInformation(STRING prefix, EsVolumeInformation *information); // Returns false if the mount point does not exist. +function EsError EsMountPointAdd(STRING prefix, EsHandle base); // The system maintains a duplicate of the base handle. +function bool EsMountPointRemove(STRING prefix); // Returns false if the mount point does not exist. You can only remove mount points you added with EsMountPointAdd. function void EsOpenDocumentQueryInformation(STRING filePath, EsOpenDocumentInformation *information); function void _EsPathAnnouncePathMoved(STRING oldPath, STRING newPath); // Set oldPathBytes = 0 to make the file manager refresh the path. function void _EsOpenDocumentEnumerate(EsBuffer *outputBuffer); @@ -2132,8 +2132,8 @@ function void EsAssertionFailure(EsCString cFile, int line); function EsCalculationValue EsCalculateFromUserExpression(EsCString cExpression); // For user input only; do not rely on consistent behaviour across versions; use with message mutex. -function_not_in_kernel void EsPanic(EsCString format, ...); -function_not_in_kernel void EsPrint(EsCString format, ...); +function void EsPanic(EsCString format, ...); +function void EsPrint(EsCString format, ...); function void EsPrintDirect(STRING string); function void EsPrintHelloWorld(); @@ -2281,7 +2281,7 @@ function const char *EsStringFormatTemporary(EsCString format, ...); // Not thr function ptrdiff_t EsStringFormatV(char *buffer, size_t bufferLength, EsCString format, va_list arguments); function bool EsStringFormatAppend(char *buffer, size_t bufferLength, size_t *bufferPosition, EsCString format, ...); // Return false if buffer filled. function bool EsStringFormatAppendV(char *buffer, size_t bufferLength, size_t *bufferPosition, EsCString format, va_list arguments); -function bool EsUTF8IsValid(const char *input, ptrdiff_t bytes); // Does not check for surrogate characters or overlong sequences of non-ASCII characters. +function bool EsUTF8IsValid(STRING input); // Does not check for surrogate characters or overlong sequences of non-ASCII characters. function double EsDoubleParse(STRING string, char **endptr); function int64_t EsIntegerParse(STRING text); // Parses in hexadecimal if the first two characters are '0x'. diff --git a/kernel/main.cpp b/kernel/main.cpp index d35bd19..74822d6 100644 --- a/kernel/main.cpp +++ b/kernel/main.cpp @@ -22,6 +22,7 @@ void KernelInitialise() { } void KernelMain(uintptr_t) { + EsPrint("Test"); desktopProcess = ProcessSpawn(PROCESS_DESKTOP); // Spawn the desktop process. DriversInitialise(); // Load the root device. ProcessStart(desktopProcess, EsLiteral(K_DESKTOP_EXECUTABLE)); // Start the desktop process. diff --git a/kernel/symbols.cpp b/kernel/symbols.cpp index 9541f00..8937a38 100644 --- a/kernel/symbols.cpp +++ b/kernel/symbols.cpp @@ -7,7 +7,7 @@ extern "C" void KernelInitialise(); extern "C" int EsStringCompareRaw(const char *s1, size_t b1, const char *s2, size_t b2); -void EsPrint(const char *format, ...); +extern "C" void EsPrint(const char *format, ...); struct ExportedKernelFunction { void *address; diff --git a/util/script.c b/util/script.c index c80fc72..b1cd719 100644 --- a/util/script.c +++ b/util/script.c @@ -417,6 +417,11 @@ void ExternalPassREPLResult(ExecutionContext *context, Value value); // --------------------------------- Base module. char baseModuleSource[] = { + // TODO Temporary. + "struct DirectoryChild { str name; bool isDirectory; int size; };" + "DirectoryChild[] Dir() { str[] names = DirectoryEnumerateChildren(\"0:\"); DirectoryChild[] result = new DirectoryChild[]; result:resize(names:len()); for int i = 0; i < names:len(); i += 1 { result[i] = new DirectoryChild; result[i].name = names[i]; result[i].isDirectory = PathIsDirectory(\"0:/\"+names[i]); result[i].size = FileGetSize(\"0:/\"+names[i]); } return result;}" + "str[] OpenDocumentEnumerate() #extcall;" + // Logging: "void PrintStdErr(str x) #extcall;" @@ -707,6 +712,7 @@ int ExternalRandomInt(ExecutionContext *context, Value *returnValue); // TODO Th int External_DirectoryInternalStartIteration(ExecutionContext *context, Value *returnValue); int External_DirectoryInternalNextIteration(ExecutionContext *context, Value *returnValue); int External_DirectoryInternalEndIteration(ExecutionContext *context, Value *returnValue); +int ExternalOpenDocumentEnumerate(ExecutionContext *context, Value *returnValue); ExternalFunction externalFunctions[] = { { .cName = "PrintStdErr", .callback = ExternalPrintStdErr }, @@ -744,6 +750,7 @@ ExternalFunction externalFunctions[] = { { .cName = "_DirectoryInternalStartIteration", .callback = External_DirectoryInternalStartIteration }, { .cName = "_DirectoryInternalNextIteration", .callback = External_DirectoryInternalNextIteration }, { .cName = "_DirectoryInternalEndIteration", .callback = External_DirectoryInternalEndIteration }, + { .cName = "OpenDocumentEnumerate", .callback = ExternalOpenDocumentEnumerate }, }; // --------------------------------- Tokenization and parsing. @@ -5749,6 +5756,12 @@ int ExternalSystemSleepMs(ExecutionContext *context, Value *returnValue) { #endif } +int ExternalOpenDocumentEnumerate(ExecutionContext *context, Value *returnValue) { + (void) context; + returnValue->i = 0; + return 3; +} + CoroutineState *ExternalCoroutineWaitAny(ExecutionContext *context) { (void) context; while (sem_wait(&externalCoroutineSemaphore) == -1);