]> git.saurik.com Git - cycript.git/blame - Syntax.cpp
Provide access to errno (though not from console).
[cycript.git] / Syntax.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
4644480a
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
4644480a 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b3378a02 19**/
4644480a
JF
20/* }}} */
21
2e43a0b0
JF
22#include <cmath>
23#include <sstream>
24
20052ff7 25#include "Syntax.hpp"
9185d5ef 26
3935b9e5 27// XXX: this implementation will break if value[size] is a digit
2e43a0b0
JF
28double CYCastDouble(const char *value, size_t size) {
29 char *end;
30 double number(strtod(value, &end));
31 if (end != value + size)
32 return NAN;
33 return number;
34}
35
36double CYCastDouble(const char *value) {
37 return CYCastDouble(value, strlen(value));
38}
39
3935b9e5
JF
40double CYCastDouble(CYUTF8String value) {
41 return CYCastDouble(value.data, value.size);
42}
43
2e43a0b0
JF
44template <>
45::pthread_key_t CYLocal<CYPool>::key_ = Key_();
46
9185d5ef
JF
47CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9
48CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$
49CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9
3935b9e5
JF
50
51
52
53// XXX: this really should not be here ... :/
54
55#include <sys/mman.h>
56#include <sys/stat.h>
57#include <fcntl.h>
58#include <unistd.h>
59
60#include "String.hpp"
61
62struct CYFile {
63 void *data_;
64 size_t size_;
65
66 CYFile(void *data, size_t size) :
67 data_(data),
68 size_(size)
69 {
70 }
71};
72
73static void CYFileExit(void *data) {
74 CYFile *file(reinterpret_cast<CYFile *>(data));
75 _syscall(munmap(file->data_, file->size_));
76}
77
78void *CYPoolFile(CYPool &pool, const char *path, size_t *psize) {
79 int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT));
80 if (fd == -1)
81 return NULL;
82
83 struct stat stat;
84 _syscall(fstat(fd, &stat));
85 size_t size(stat.st_size);
86
87 *psize = size;
88
89 void *base;
90 if (size == 0)
91 base = pool.strndup("", 0);
92 else {
93 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
94
95 CYFile *file(new (pool) CYFile(base, size));
96 pool.atexit(&CYFileExit, file);
97 }
98
99 _syscall(close(fd));
100 return base;
101}
102
103CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path) {
104 CYUTF8String data;
105 data.data = reinterpret_cast<char *>(CYPoolFile(pool, path, &data.size));
106 return data;
107}