#include "Internal.hpp"
#include <dlfcn.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "cycript.hpp"
#include "Execute.hpp"
#include <sys/mman.h>
+#include <sys/stat.h>
#include <iostream>
#include <set>
#include "Parser.hpp"
+#include "Decode.hpp"
#include "Error.hpp"
#include "JavaScript.hpp"
#include "String.hpp"
bool comma(false);
for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) {
- JSValueRef value(CYGetProperty(context, _this, index));
-
if (comma)
str << ',';
else
comma = true;
- if (!JSValueIsUndefined(context, value))
- str << CYPoolCCYON(pool, context, value);
- else {
- str << ',';
- comma = false;
+ try {
+ JSValueRef value(CYGetProperty(context, _this, index));
+ if (!JSValueIsUndefined(context, value))
+ str << CYPoolCCYON(pool, context, value);
+ else {
+ str << ',';
+ comma = false;
+ }
+ } catch (const CYException &error) {
+ str << "@error";
}
}
return CYCallFunction(pool, context, 0, NULL, count, arguments, false, &internal->signature_, &internal->cif_, internal->GetValue());
} CYCatch(NULL) }
-JSObjectRef CYMakeType(JSContextRef context, const char *type) {
- Type_privateData *internal(new Type_privateData(type));
+JSObjectRef CYMakeType(JSContextRef context, const char *encoding) {
+ Type_privateData *internal(new Type_privateData(encoding));
return JSObjectMake(context, Type_privateData::Class_, internal);
}
return JSObjectMake(context, Type_privateData::Class_, internal);
}
+JSObjectRef CYMakeType(JSContextRef context, sig::Signature *signature) {
+ CYPool pool;
+
+ sig::Type type;
+ type.name = NULL;
+ type.flags = 0;
+
+ type.primitive = sig::function_P;
+ sig::Copy(pool, type.data.signature, *signature);
+
+ return CYMakeType(context, &type);
+}
+
static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
JSObjectRef global(CYGetGlobalObject(context));
JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
return CYMakeType(context, &type);
} CYCatch(NULL) }
+static JSValueRef Type_callAsFunction_functionWith(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+
+ CYPool pool;
+
+ sig::Type type;
+ type.name = NULL;
+ type.flags = 0;
+
+ type.primitive = sig::function_P;
+ type.data.signature.elements = new(pool) sig::Element[1 + count];
+ type.data.signature.count = 1 + count;
+
+ type.data.signature.elements[0].name = NULL;
+ type.data.signature.elements[0].type = internal->type_;
+ type.data.signature.elements[0].offset = _not(size_t);
+
+ for (size_t i(0); i != count; ++i) {
+ sig::Element &element(type.data.signature.elements[i + 1]);
+ element.name = NULL;
+ element.offset = _not(size_t);
+
+ JSObjectRef object(CYCastJSObject(context, arguments[i]));
+ _assert(JSValueIsObjectOfClass(context, object, Type_privateData::Class_));
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+
+ element.type = internal->type_;
+ }
+
+ return CYMakeType(context, &type);
+} CYCatch(NULL) }
+
static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
if (count != 0)
throw CYJSError(context, "incorrect number of arguments to Type.pointerTo");
sig::Type type;
type.name = NULL;
- type.flags = 0;
- type.primitive = sig::pointer_P;
- type.data.data.type = internal->type_;
- type.data.data.size = 0;
+ if (internal->type_->primitive == sig::char_P) {
+ type.flags = internal->type_->flags;
+ type.primitive = sig::string_P;
+ type.data.data.type = NULL;
+ type.data.data.size = 0;
+ } else {
+ type.flags = 0;
+ type.primitive = sig::pointer_P;
+ type.data.data.type = internal->type_;
+ type.data.data.size = 0;
+ }
return CYMakeType(context, &type);
} CYCatch(NULL) }
static JSValueRef Functor_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
- CYPool pool;
- return CYCastJSValue(context, Unparse(pool, &internal->signature_));
+ return CYMakeType(context, &internal->signature_);
} CYCatch(NULL) }
static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
- CYPool pool;
- const char *type(sig::Unparse(pool, internal->type_));
- std::ostringstream str;
- CYStringify(str, type, strlen(type));
- char *cyon(pool.strcat("new Type(", str.str().c_str(), ")", NULL));
- return CYCastJSValue(context, CYJSString(cyon));
+ CYLocalPool pool;
+ std::ostringstream out;
+ CYOptions options;
+ CYOutput output(out, options);
+ (new(pool) CYEncodedType(Decode(pool, internal->type_)))->Output(output, CYNoFlags);
+ return CYCastJSValue(context, CYJSString(out.str().c_str()));
} CYCatch(NULL) }
static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
{NULL, NULL, NULL, 0}
};
-static JSStaticFunction Type_staticFunctions[8] = {
+static JSStaticFunction Type_staticFunctions[9] = {
{"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"functionWith", &Type_callAsFunction_functionWith, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
return JSContextGetGlobalObject(context);
}
-const char *CYExecute(CYPool &pool, CYUTF8String code) {
- JSContextRef context(CYGetJSContext());
- JSValueRef exception(NULL), result;
+const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
+ JSValueRef exception(NULL);
void *handle;
if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
else
handle = NULL;
- const char *json;
+ try {
+ JSValueRef result;
try {
result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
} catch (const char *error) {
if (JSValueIsUndefined(context, result))
return NULL;
+ const char *json;
try {
json = CYPoolCCYON(pool, context, result, &exception);
} catch (const char *error) {
CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
- if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
- (*hooks_->ExecuteEnd)(context, handle);
return json;
+
+ } catch (...) {
+ if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
+ (*hooks_->ExecuteEnd)(context, handle);
+ throw;
+ }
}
extern "C" void CydgetSetupContext(JSGlobalContextRef context) {
return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_;
}
+extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size);
+
+void *CYMapFile(const char *path, size_t *psize) {
+ int fd;
+ _syscall(fd = open(path, O_RDONLY));
+
+ struct stat stat;
+ _syscall(fstat(fd, &stat));
+ size_t size(stat.st_size);
+
+ *psize = size;
+
+ void *base;
+ _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
+
+ _syscall(close(fd));
+ return base;
+}
+
+static void CYRunSetups(JSContextRef context) {
+ std::string folder("/etc/cycript/setup.d");
+ DIR *setups(opendir(folder.c_str()));
+ if (setups == NULL)
+ return;
+
+ for (;;) {
+ dirent setup;
+ dirent *result;
+ _syscall(readdir_r(setups, &setup, &result));
+
+ if (result == NULL)
+ break;
+ _assert(result == &setup);
+
+ const char *name(setup.d_name);
+ size_t length(strlen(name));
+ if (length < 4)
+ continue;
+
+ if (name[0] == '.')
+ continue;
+ if (memcmp(name + length - 3, ".cy", 3) != 0)
+ continue;
+
+ std::string script(folder + "/" + name);
+ CYUTF8String utf8;
+ utf8.data = reinterpret_cast<char *>(CYMapFile(script.c_str(), &utf8.size));
+
+ CYPool pool;
+ CYUTF16String utf16(CYPoolUTF16String(pool, utf8));
+ munmap(const_cast<char *>(utf8.data), utf8.size);
+
+ if (CydgetMemoryParse(&utf16.data, &utf16.size))
+ CYExecute(context, pool, CYPoolUTF8String(pool, utf16));
+ free(const_cast<uint16_t *>(utf16.data));
+ }
+
+ _syscall(closedir(setups));
+}
+
extern "C" void CYSetupContext(JSGlobalContextRef context) {
CYInitializeDynamic();
(*hooks_->SetupContext)(context);
CYArrayPush(context, alls, cycript);
+
+ CYRunSetups(context);
}
JSGlobalContextRef CYGetJSContext() {