X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/20052ff75c224699725da0f8e096053009c62741..4b5dc402335c630735ef8e790e6d117e10275a56:/Syntax.cpp diff --git a/Syntax.cpp b/Syntax.cpp index c2e84e8..c568102 100644 --- a/Syntax.cpp +++ b/Syntax.cpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2015 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -19,8 +19,89 @@ **/ /* }}} */ +#include +#include + #include "Syntax.hpp" +// XXX: this implementation will break if value[size] is a digit +double CYCastDouble(const char *value, size_t size) { + char *end; + double number(strtod(value, &end)); + if (end != value + size) + return NAN; + return number; +} + +double CYCastDouble(const char *value) { + return CYCastDouble(value, strlen(value)); +} + +double CYCastDouble(CYUTF8String value) { + return CYCastDouble(value.data, value.size); +} + +template <> +::pthread_key_t CYLocal::key_ = Key_(); + CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 + + + +// XXX: this really should not be here ... :/ + +#include +#include +#include +#include + +#include "String.hpp" + +struct CYFile { + void *data_; + size_t size_; + + CYFile(void *data, size_t size) : + data_(data), + size_(size) + { + } +}; + +static void CYFileExit(void *data) { + CYFile *file(reinterpret_cast(data)); + _syscall(munmap(file->data_, file->size_)); +} + +void *CYPoolFile(CYPool &pool, const char *path, size_t *psize) { + int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT)); + if (fd == -1) + return NULL; + + struct stat stat; + _syscall(fstat(fd, &stat)); + size_t size(stat.st_size); + + *psize = size; + + void *base; + if (size == 0) + base = pool.strndup("", 0); + else { + _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); + + CYFile *file(new (pool) CYFile(base, size)); + pool.atexit(&CYFileExit, file); + } + + _syscall(close(fd)); + return base; +} + +CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path) { + CYUTF8String data; + data.data = reinterpret_cast(CYPoolFile(pool, path, &data.size)); + return data; +}