本文整理汇总了C++中JSStringCreateWithUTF8CString函数的典型用法代码示例。如果您正苦于以下问题:C++ JSStringCreateWithUTF8CString函数的具体用法?C++ JSStringCreateWithUTF8CString怎么用?C++ JSStringCreateWithUTF8CString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了JSStringCreateWithUTF8CString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: keyDownCallback
static JSValueRef keyDownCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
if (argumentCount < 1)
return JSValueMakeUndefined(context);
static JSStringRef ctrlKey = JSStringCreateWithUTF8CString("ctrlKey");
static JSStringRef shiftKey = JSStringCreateWithUTF8CString("shiftKey");
static JSStringRef altKey = JSStringCreateWithUTF8CString("altKey");
static JSStringRef metaKey = JSStringCreateWithUTF8CString("metaKey");
static JSStringRef lengthProperty = JSStringCreateWithUTF8CString("length");
COMPtr<IWebFramePrivate> framePrivate;
if (SUCCEEDED(frame->QueryInterface(&framePrivate)))
framePrivate->layout();
JSStringRef character = JSValueToStringCopy(context, arguments[0], exception);
ASSERT(!exception || !*exception);
int charCode = JSStringGetCharactersPtr(character)[0];
int virtualKeyCode = toupper(LOBYTE(VkKeyScan(charCode)));
JSStringRelease(character);
// Hack to map option-delete to ctrl-delete
// Remove this when we fix <rdar://problem/5102974> layout tests need a way to decide how to choose the appropriate modifier keys
bool convertOptionToCtrl = false;
if (virtualKeyCode == VK_DELETE || virtualKeyCode == VK_BACK)
convertOptionToCtrl = true;
BYTE keyState[256];
if (argumentCount > 1) {
::GetKeyboardState(keyState);
BYTE newKeyState[256];
memcpy(newKeyState, keyState, sizeof(keyState));
JSObjectRef modifiersArray = JSValueToObject(context, arguments[1], exception);
if (modifiersArray) {
int modifiersCount = JSValueToNumber(context, JSObjectGetProperty(context, modifiersArray, lengthProperty, 0), 0);
for (int i = 0; i < modifiersCount; ++i) {
JSValueRef value = JSObjectGetPropertyAtIndex(context, modifiersArray, i, 0);
JSStringRef string = JSValueToStringCopy(context, value, 0);
if (JSStringIsEqual(string, ctrlKey))
newKeyState[VK_CONTROL] = 0x80;
else if (JSStringIsEqual(string, shiftKey))
newKeyState[VK_SHIFT] = 0x80;
else if (JSStringIsEqual(string, altKey)) {
if (convertOptionToCtrl)
newKeyState[VK_CONTROL] = 0x80;
else
newKeyState[VK_MENU] = 0x80;
} else if (JSStringIsEqual(string, metaKey))
newKeyState[VK_MENU] = 0x80;
JSStringRelease(string);
}
}
::SetKeyboardState(newKeyState);
}
MSG msg = makeMsg(webViewWindow, WM_KEYDOWN, virtualKeyCode, 0);
dispatchMessage(&msg);
if (argumentCount > 1)
::SetKeyboardState(keyState);
return JSValueMakeUndefined(context);
}
示例2: main
int main(int argc, char* argv[])
{
const char *scriptPath = "testapi.js";
if (argc > 1) {
scriptPath = argv[1];
}
// Test garbage collection with a fresh context
context = JSGlobalContextCreateInGroup(NULL, NULL);
TestInitializeFinalize = true;
testInitializeFinalize();
JSGlobalContextRelease(context);
TestInitializeFinalize = false;
ASSERT(Base_didFinalize);
JSClassDefinition globalObjectClassDefinition = kJSClassDefinitionEmpty;
globalObjectClassDefinition.initialize = globalObject_initialize;
globalObjectClassDefinition.staticValues = globalObject_staticValues;
globalObjectClassDefinition.staticFunctions = globalObject_staticFunctions;
globalObjectClassDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
JSClassRef globalObjectClass = JSClassCreate(&globalObjectClassDefinition);
context = JSGlobalContextCreateInGroup(NULL, globalObjectClass);
JSGlobalContextRetain(context);
JSGlobalContextRelease(context);
JSObjectRef globalObject = JSContextGetGlobalObject(context);
ASSERT(JSValueIsObject(context, globalObject));
JSValueRef jsUndefined = JSValueMakeUndefined(context);
JSValueRef jsNull = JSValueMakeNull(context);
JSValueRef jsTrue = JSValueMakeBoolean(context, true);
JSValueRef jsFalse = JSValueMakeBoolean(context, false);
JSValueRef jsZero = JSValueMakeNumber(context, 0);
JSValueRef jsOne = JSValueMakeNumber(context, 1);
JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0);
JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, NULL);
JSObjectSetPrototype(context, jsObjectNoProto, JSValueMakeNull(context));
// FIXME: test funny utf8 characters
JSStringRef jsEmptyIString = JSStringCreateWithUTF8CString("");
JSValueRef jsEmptyString = JSValueMakeString(context, jsEmptyIString);
JSStringRef jsOneIString = JSStringCreateWithUTF8CString("1");
JSValueRef jsOneString = JSValueMakeString(context, jsOneIString);
UniChar singleUniChar = 65; // Capital A
CFMutableStringRef cfString =
CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault,
&singleUniChar,
1,
1,
kCFAllocatorNull);
JSStringRef jsCFIString = JSStringCreateWithCFString(cfString);
JSValueRef jsCFString = JSValueMakeString(context, jsCFIString);
CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8);
JSStringRef jsCFEmptyIString = JSStringCreateWithCFString(cfEmptyString);
JSValueRef jsCFEmptyString = JSValueMakeString(context, jsCFEmptyIString);
CFIndex cfStringLength = CFStringGetLength(cfString);
UniChar* buffer = (UniChar*)malloc(cfStringLength * sizeof(UniChar));
CFStringGetCharacters(cfString,
CFRangeMake(0, cfStringLength),
buffer);
JSStringRef jsCFIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, cfStringLength);
JSValueRef jsCFStringWithCharacters = JSValueMakeString(context, jsCFIStringWithCharacters);
JSStringRef jsCFEmptyIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, CFStringGetLength(cfEmptyString));
free(buffer);
JSValueRef jsCFEmptyStringWithCharacters = JSValueMakeString(context, jsCFEmptyIStringWithCharacters);
ASSERT(JSValueGetType(context, jsUndefined) == kJSTypeUndefined);
ASSERT(JSValueGetType(context, jsNull) == kJSTypeNull);
ASSERT(JSValueGetType(context, jsTrue) == kJSTypeBoolean);
ASSERT(JSValueGetType(context, jsFalse) == kJSTypeBoolean);
ASSERT(JSValueGetType(context, jsZero) == kJSTypeNumber);
ASSERT(JSValueGetType(context, jsOne) == kJSTypeNumber);
ASSERT(JSValueGetType(context, jsOneThird) == kJSTypeNumber);
ASSERT(JSValueGetType(context, jsEmptyString) == kJSTypeString);
ASSERT(JSValueGetType(context, jsOneString) == kJSTypeString);
ASSERT(JSValueGetType(context, jsCFString) == kJSTypeString);
ASSERT(JSValueGetType(context, jsCFStringWithCharacters) == kJSTypeString);
ASSERT(JSValueGetType(context, jsCFEmptyString) == kJSTypeString);
ASSERT(JSValueGetType(context, jsCFEmptyStringWithCharacters) == kJSTypeString);
JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject");
JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL);
JSStringRelease(myObjectIString);
JSValueRef exception;
// Conversions that throw exceptions
exception = NULL;
ASSERT(NULL == JSValueToObject(context, jsNull, &exception));
ASSERT(exception);
//.........这里部分代码省略.........
示例3: pdf_jsimp_execute
void pdf_jsimp_execute(pdf_jsimp *imp, char *code)
{
JSStringRef jcode = JSStringCreateWithUTF8CString(code);
JSEvaluateScript(imp->jscore_ctx, jcode, NULL, NULL, 0, NULL);
JSStringRelease(jcode);
}
示例4: applicationControllerStr
void ApplicationTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
{
JSRetainPtr<JSStringRef> applicationControllerStr(Adopt, JSStringCreateWithUTF8CString("applicationTestController"));
JSValueRef applicationControllerObject = JSObjectMake(context, getJSClass(), this);
JSObjectSetProperty(context, windowObject, applicationControllerStr.get(), applicationControllerObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
}
示例5: init_widget_js_obj
static void
init_widget_js_obj (void *context, struct widget *widget) {
char classname[64];
snprintf(classname, 64, "widget_%s", widget->type);
const JSClassDefinition widget_js_def = {
.className = classname,
.staticFunctions = widget->js_staticfuncs,
};
JSClassRef class_def = JSClassCreate(&widget_js_def);
JSObjectRef class_obj = JSObjectMake(context, class_def, widget);
JSObjectRef global_obj = JSContextGetGlobalObject(context);
JSStringRef str_name = JSStringCreateWithUTF8CString(classname);
JSObjectSetProperty(context, global_obj, str_name, class_obj, kJSPropertyAttributeNone, NULL);
JSStringRelease(str_name);
widget->js_context = context;
widget->js_object = class_obj;
}
static struct widget*
spawn_widget (struct bar *bar, void *context, json_t *config, const char *name) {
widget_main_t widget_main;
widget_type_t widget_type;
char libname[64];
snprintf(libname, 64, "libwidget_%s", name);
gchar *libpath = g_module_build_path(LIBDIR, libname);
GModule *lib = g_module_open(libpath, G_MODULE_BIND_LOCAL);
pthread_t return_thread;
struct widget *widget = calloc(1, sizeof(struct widget));
if (lib == NULL) {
LOG_WARN("loading of '%s' (%s) failed", libpath, name);
goto error;
}
if (!g_module_symbol(lib, "widget_main", (void*)&widget_main)) {
LOG_WARN("loading of '%s' (%s) failed: unable to load module symbol", libpath, name);
goto error;
}
JSStaticFunction *js_staticfuncs = calloc(1, sizeof(JSStaticFunction));
if (g_module_symbol(lib, "widget_js_staticfuncs", (void*)&js_staticfuncs)) {
widget->js_staticfuncs = js_staticfuncs;
}
else {
free(js_staticfuncs);
}
widget->bar = bar;
widget->config = config;
widget->name = strdup(name);
pthread_mutex_init(&widget->exit_mutex, NULL);
pthread_cond_init(&widget->exit_cond, NULL);
if (g_module_symbol(lib, "widget_type", (void*)&widget_type)) {
widget->type = widget_type();
}
else {
widget->type = widget->name;
}
init_widget_js_obj(context, widget);
if (pthread_create(&return_thread, NULL, (void*(*)(void*))widget_main, widget) != 0) {
LOG_ERR("failed to start widget %s: %s", name, strerror(errno));
goto error;
}
widget->thread = return_thread;
return widget;
error:
if (widget->name != NULL) {
free(widget->name);
}
if (widget->js_staticfuncs != NULL) {
free(widget->js_staticfuncs);
}
free(widget);
return 0;
}
示例6: putForJSBuffer
/**
* put
*/
JSValueRef putForJSBuffer (JSContextRef ctx, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
BUFFER(buffer);
ARGCOUNT(4);
JSValueRef bufValueRef = arguments[0];
if (!JSValueIsObject(ctx,bufValueRef))
{
JSStringRef string = JSStringCreateWithUTF8CString("first argument must be a buffer object");
JSValueRef message = JSValueMakeString(ctx, string);
JSStringRelease(string);
*exception = JSObjectMakeError(ctx, 1, &message, 0);
return JSValueMakeUndefined(ctx);
}
JSObjectRef bufObjectRef = JSValueToObject(ctx,bufValueRef,exception);
CHECK_EXCEPTION_UNDEFINED
JSBuffer *srcBuffer = (JSBuffer*)HyperloopGetPrivateObjectAsJSBuffer(bufObjectRef);
if (srcBuffer==nullptr)
{
JSStringRef string = JSStringCreateWithUTF8CString("first argument must be a buffer object (JSBuffer nullptr)");
JSValueRef message = JSValueMakeString(ctx, string);
JSStringRelease(string);
*exception = JSObjectMakeError(ctx, 1, &message, 0);
return JSValueMakeUndefined(ctx);
}
GET_NUMBER(1,srcIndex);
GET_NUMBER(2,srcLength);
GET_NUMBER(3,destIndex);
if (srcLength > srcBuffer->length)
{
JSStringRef string = JSStringCreateWithUTF8CString("source length passed in greater than source buffer length");
JSValueRef message = JSValueMakeString(ctx, string);
JSStringRelease(string);
*exception = JSObjectMakeError(ctx, 1, &message, 0);
return JSValueMakeUndefined(ctx);
}
if (srcLength <= 0)
{
JSStringRef string = JSStringCreateWithUTF8CString("source length must be greater than 0");
JSValueRef message = JSValueMakeString(ctx, string);
JSStringRelease(string);
*exception = JSObjectMakeError(ctx, 1, &message, 0);
return JSValueMakeUndefined(ctx);
}
throw ref new Exception(0, "JSBuffer's putForJSBuffer has not been implemented on Windows yet.");
/*void *src = &(srcBuffer->buffer[(int)srcIndex]);
size_t newsize = (buffer->length - (int)destIndex);
newsize = newsize + srcLength - newsize;
void *dest = &(buffer->buffer[(int)destIndex]);
if (newsize > buffer->length)
{
// new to grow it
void *copy = malloc(buffer->length);
size_t copylen = buffer->length;
memcpy(copy, buffer->buffer, copylen);
free(buffer->buffer);
buffer->buffer = malloc(newsize);
buffer->length = newsize;
memcpy(buffer->buffer,copy,copylen);
}
memcpy(dest, src, (int)srcLength);
return object;*/
}
示例7: main
gint
main (gint argc, gchar ** argv)
{
JSGlobalContextRef ctx;
JSStringRef str;
JSObjectRef func;
JSValueRef result;
JSValueRef exception = NULL;
JsonObject *obj;
JsonNode *node;
g_type_init();
gchar *buffer;
if (argc != 2)
{
printf ("Usage: %s <script>\n", argv[0]);
return 1;
}
ctx = JSGlobalContextCreate (NULL);
str = JSStringCreateWithUTF8CString ("emitIntermediate");
func = JSObjectMakeFunctionWithCallback (ctx, str, js_emitIntermediate);
JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func,
kJSPropertyAttributeNone, NULL);
JSStringRelease (str);
str = JSStringCreateWithUTF8CString ("emit");
func = JSObjectMakeFunctionWithCallback (ctx, str, js_emit);
JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func,
kJSPropertyAttributeNone, NULL);
JSStringRelease (str);
str = JSStringCreateWithUTF8CString (argv[1]);
result = JSEvaluateScript (ctx, str, NULL, NULL, 0, &exception);
JSStringRelease (str);
obj = json_object_new ();
if (!result || exception)
{
js_value (ctx, exception, &node);
json_object_set_member (obj, "error", node);
json_object_set_boolean_member (obj, "status", FALSE);
JSGlobalContextRelease (ctx);
}
else
{
json_object_set_boolean_member (obj, "status", TRUE);
}
JsonNode *node1 = json_node_new (JSON_NODE_OBJECT);
if (node1 == NULL)
{
json_object_unref (obj);
JSGlobalContextRelease (ctx);
return 1;
}
json_node_set_object (node1, obj);
JsonGenerator *gen = json_generator_new();
if (gen == NULL)
{
json_node_free (node1);
JSGlobalContextRelease (ctx);
return 1;
}
json_generator_set_root (gen, node1 );
buffer = json_generator_to_data (gen,NULL);
if (buffer == NULL)
{
json_node_free (node1);
JSGlobalContextRelease (ctx);
return 1;
}
json_node_free (node1);
puts (buffer);
g_free (buffer);
JSGlobalContextRelease (ctx);
return 0;
}
示例8: c_string_to_value
JSValueRef c_string_to_value(JSContextRef ctx, const char *s) {
JSStringRef str = JSStringCreateWithUTF8CString(s);
return JSValueMakeString(ctx, str);
}
示例9: array_get_count
int array_get_count(JSContextRef ctx, JSObjectRef arr) {
JSStringRef pname = JSStringCreateWithUTF8CString("length");
JSValueRef val = JSObjectGetProperty(ctx, arr, pname, NULL);
JSStringRelease(pname);
return (int) JSValueToNumber(ctx, val, NULL);
}
示例10: tester_js_test
static const JSClassDefinition tester_def = {
0,
kJSClassAttributeNone,
"TesterClass",
NULL,
NULL,
tester_static_funcs,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
static JSValueRef tester_js_test(JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *ex) {
JSStringRef str = JSStringCreateWithUTF8CString("Test!");
return JSValueMakeString(ctx, str);
}
/*
* Adds Tester class to a context
*/
void add_tester_js_class() {
cwebkit_add_js_class_to_list(tester_def, JSStringCreateWithUTF8CString("tester"));
}
示例11: function_load
JSValueRef function_load(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception) {
// TODO: implement fully
if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) {
char path[PATH_MAX];
JSStringRef path_str = JSValueToStringCopy(ctx, args[0], NULL);
assert(JSStringGetLength(path_str) < PATH_MAX);
JSStringGetUTF8CString(path_str, path, PATH_MAX);
JSStringRelease(path_str);
// debug_print_value("load", ctx, args[0]);
time_t last_modified = 0;
char *contents = NULL;
char *loaded_path = strdup(path);
bool developing = (config.num_src_paths == 1 &&
strcmp(config.src_paths[0].type, "src") == 0 &&
str_has_suffix(config.src_paths[0].path, "/planck-cljs/src/") == 0);
if (!developing) {
contents = bundle_get_contents(path);
last_modified = 0;
}
// load from classpath
if (contents == NULL) {
for (int i = 0; i < config.num_src_paths; i++) {
if (config.src_paths[i].blacklisted) {
continue;
}
char *type = config.src_paths[i].type;
char *location = config.src_paths[i].path;
if (strcmp(type, "src") == 0) {
char *full_path = str_concat(location, path);
contents = get_contents(full_path, &last_modified);
if (contents != NULL) {
free(loaded_path);
loaded_path = strdup(full_path);
}
free(full_path);
} else if (strcmp(type, "jar") == 0) {
struct stat file_stat;
if (stat(location, &file_stat) == 0) {
contents = get_contents_zip(location, path, &last_modified);
} else {
cljs_perror(location);
config.src_paths[i].blacklisted = true;
}
}
if (contents != NULL) {
break;
}
}
}
// load from out/
if (contents == NULL) {
if (config.out_path != NULL) {
char *full_path = str_concat(config.out_path, path);
contents = get_contents(full_path, &last_modified);
free(full_path);
}
}
if (developing && contents == NULL) {
contents = bundle_get_contents(path);
last_modified = 0;
}
if (contents != NULL) {
JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
free(contents);
JSStringRef loaded_path_str = JSStringCreateWithUTF8CString(loaded_path);
free(loaded_path);
JSValueRef res[3];
res[0] = JSValueMakeString(ctx, contents_str);
res[1] = JSValueMakeNumber(ctx, last_modified);
res[2] = JSValueMakeString(ctx, loaded_path_str);
return JSObjectMakeArray(ctx, 3, res, NULL);
}
}
return JSValueMakeNull(ctx);
}
示例12: if
JSValueRef JSCInt16Array::setCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) {
struct JSCInt16ArrayPrivate* privData = (struct JSCInt16ArrayPrivate*)JSObjectGetPrivate(thisObj);
if (false) {
} else if (argumentCount == 2 &&
JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCInt16Array::getTmpl()) &&
JSValueIsNumber(ctx, arguments[1])) {
uscxml::Int16Array* localArray = ((struct JSCInt16Array::JSCInt16ArrayPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj;
unsigned long localOffset = (unsigned long)JSValueToNumber(ctx, arguments[1], exception);
privData->nativeObj->set(localArray, localOffset);
JSValueRef jscRetVal = JSValueMakeUndefined(ctx);
return jscRetVal;
} else if (argumentCount == 2 &&
JSValueIsNumber(ctx, arguments[0]) &&
JSValueIsNumber(ctx, arguments[1])) {
unsigned long localIndex = (unsigned long)JSValueToNumber(ctx, arguments[0], exception);
short localValue = (short)JSValueToNumber(ctx, arguments[1], exception);
privData->nativeObj->set(localIndex, localValue);
JSValueRef jscRetVal = JSValueMakeUndefined(ctx);
return jscRetVal;
} else if (argumentCount == 2 &&
JSValueIsObject(ctx, arguments[0]) &&
JSValueIsNumber(ctx, arguments[1])) {
std::vector<short> localArray;
JSValueRef localArrayItem;
unsigned int localArrayIndex = 0;
while((localArrayItem = JSObjectGetPropertyAtIndex(ctx, JSValueToObject(ctx, arguments[0], exception), localArrayIndex, exception))) {
if (JSValueIsUndefined(ctx, localArrayItem))
break;
if (JSValueIsNumber(ctx,localArrayItem))
localArray.push_back(JSValueToNumber(ctx, localArrayItem, exception));
localArrayIndex++;
}
unsigned long localOffset = (unsigned long)JSValueToNumber(ctx, arguments[1], exception);
privData->nativeObj->set(localArray, localOffset);
JSValueRef jscRetVal = JSValueMakeUndefined(ctx);
return jscRetVal;
} else if (argumentCount == 1 &&
JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCInt16Array::getTmpl())) {
uscxml::Int16Array* localArray = ((struct JSCInt16Array::JSCInt16ArrayPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj;
privData->nativeObj->set(localArray);
JSValueRef jscRetVal = JSValueMakeUndefined(ctx);
return jscRetVal;
} else if (argumentCount == 1 &&
JSValueIsObject(ctx, arguments[0])) {
std::vector<short> localArray;
JSValueRef localArrayItem;
unsigned int localArrayIndex = 0;
while((localArrayItem = JSObjectGetPropertyAtIndex(ctx, JSValueToObject(ctx, arguments[0], exception), localArrayIndex, exception))) {
if (JSValueIsUndefined(ctx, localArrayItem))
break;
if (JSValueIsNumber(ctx,localArrayItem))
localArray.push_back(JSValueToNumber(ctx, localArrayItem, exception));
localArrayIndex++;
}
privData->nativeObj->set(localArray);
JSValueRef jscRetVal = JSValueMakeUndefined(ctx);
return jscRetVal;
}
JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling set");
*exception = JSValueMakeString(ctx, exceptionString);
JSStringRelease(exceptionString);
return JSValueMakeUndefined(ctx);
}
示例13: function_http_request
JSValueRef function_http_request(JSContextRef ctx, JSObjectRef function, JSObjectRef this_object,
size_t argc, const JSValueRef args[], JSValueRef* exception) {
if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeObject) {
JSObjectRef opts = JSValueToObject(ctx, args[0], NULL);
JSValueRef url_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("url"), NULL);
char *url = value_to_c_string(ctx, url_ref);
JSValueRef timeout_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("timeout"), NULL);
time_t timeout = 0;
if (JSValueIsNumber(ctx, timeout_ref)) {
timeout = (time_t) JSValueToNumber(ctx, timeout_ref, NULL);
}
JSValueRef method_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("method"), NULL);
char *method = value_to_c_string(ctx, method_ref);
JSValueRef body_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("body"), NULL);
JSObjectRef headers_obj = JSValueToObject(ctx, JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("headers"), NULL), NULL);
CURL *handle = curl_easy_init();
assert(handle != NULL);
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method);
curl_easy_setopt(handle, CURLOPT_URL, url);
struct curl_slist *headers = NULL;
if (!JSValueIsNull(ctx, headers_obj)) {
JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(ctx, headers_obj);
int n = JSPropertyNameArrayGetCount(properties);
for (int i = 0; i < n; i++) {
JSStringRef key_str = JSPropertyNameArrayGetNameAtIndex(properties, i);
JSValueRef val_ref = JSObjectGetProperty(ctx, headers_obj, key_str, NULL);
int len = JSStringGetLength(key_str) + 1;
char *key = malloc(len * sizeof(char));
JSStringGetUTF8CString(key_str, key, len);
JSStringRef val_as_str = to_string(ctx, val_ref);
char *val = value_to_c_string(ctx, JSValueMakeString(ctx, val_as_str));
JSStringRelease(val_as_str);
int len_key = strlen(key);
int len_val = strlen(val);
char *header = malloc((len_key + len_val + 2 + 1) * sizeof(char));
sprintf(header, "%s: %s", key, val);
curl_slist_append(headers, header);
free(header);
free(key);
free(val);
}
curl_easy_setopt(handle, CURLOPT_HEADER, headers);
}
// curl_easy_setopt(handle, CURLOPT_HEADER, 1L);
curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);
struct read_string_state input_state;
if (!JSValueIsUndefined(ctx, body_ref)) {
char *body = value_to_c_string(ctx, body_ref);
input_state.input = body;
input_state.offset = 0;
input_state.length = strlen(body);
curl_easy_setopt(handle, CURLOPT_READDATA, &input_state);
curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_string_callback);
}
JSObjectRef response_headers = JSObjectMake(ctx, NULL, NULL);
struct header_state header_state;
header_state.ctx = ctx;
header_state.headers = response_headers;
curl_easy_setopt(handle, CURLOPT_HEADERDATA, &header_state);
curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_to_object_callback);
struct write_state body_state;
body_state.offset = 0;
body_state.length = 0;
body_state.data = NULL;
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &body_state);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_string_callback);
JSObjectRef result = JSObjectMake(ctx, NULL, NULL);
int res = curl_easy_perform(handle);
if (res != 0) {
JSStringRef error_str = JSStringCreateWithUTF8CString(curl_easy_strerror(res));
JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("error"), JSValueMakeString(ctx, error_str), kJSPropertyAttributeReadOnly, NULL);
}
int status = 0;
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
// printf("%d bytes, %x\n", body_state.offset, body_state.data);
if (body_state.data != NULL) {
JSStringRef body_str = JSStringCreateWithUTF8CString(body_state.data);
JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("body"), JSValueMakeString(ctx, body_str), kJSPropertyAttributeReadOnly, NULL);
free(body_state.data);
}
JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("status"), JSValueMakeNumber(ctx, status), kJSPropertyAttributeReadOnly, NULL);
JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("headers"), response_headers, kJSPropertyAttributeReadOnly, NULL);
//.........这里部分代码省略.........
示例14: setExceptionForString
static void setExceptionForString(JSContextRef context, JSValueRef* exception, const char* string)
{
JSRetainPtr<JSStringRef> exceptionString(Adopt, JSStringCreateWithUTF8CString(string));
*exception = JSValueMakeString(context, exceptionString.get());
}
示例15: window_object_cleared_callback
static void
window_object_cleared_callback(WebKitScriptWorld *world,
WebKitWebPage *web_page,
WebKitFrame *frame,
LightDMGreeter *greeter) {
JSObjectRef gettext_object, lightdm_greeter_object, config_file_object, greeter_util_object;
JSGlobalContextRef jsContext;
JSObjectRef globalObject;
WebKitDOMDocument *dom_document;
WebKitDOMDOMWindow *dom_window;
gchar *message = "LockHint";
page_id = webkit_web_page_get_id(web_page);
jsContext = webkit_frame_get_javascript_context_for_script_world(frame, world);
globalObject = JSContextGetGlobalObject(jsContext);
gettext_class = JSClassCreate(&gettext_definition);
lightdm_greeter_class = JSClassCreate(&lightdm_greeter_definition);
lightdm_user_class = JSClassCreate(&lightdm_user_definition);
lightdm_language_class = JSClassCreate(&lightdm_language_definition);
lightdm_layout_class = JSClassCreate(&lightdm_layout_definition);
lightdm_session_class = JSClassCreate(&lightdm_session_definition);
config_file_class = JSClassCreate(&config_file_definition);
greeter_util_class = JSClassCreate(&greeter_util_definition);
gettext_object = JSObjectMake(jsContext, gettext_class, NULL);
JSObjectSetProperty(jsContext,
globalObject,
JSStringCreateWithUTF8CString("gettext"),
gettext_object,
kJSPropertyAttributeNone,
NULL);
lightdm_greeter_object = JSObjectMake(jsContext, lightdm_greeter_class, greeter);
JSObjectSetProperty(jsContext,
globalObject,
JSStringCreateWithUTF8CString("lightdm"),
lightdm_greeter_object,
kJSPropertyAttributeNone,
NULL);
config_file_object = JSObjectMake(jsContext, config_file_class, greeter);
JSObjectSetProperty(jsContext,
globalObject,
JSStringCreateWithUTF8CString("config"),
config_file_object,
kJSPropertyAttributeNone,
NULL);
greeter_util_object = JSObjectMake(jsContext, greeter_util_class, NULL);
JSObjectSetProperty(jsContext,
globalObject,
JSStringCreateWithUTF8CString("greeterutil"),
greeter_util_object,
kJSPropertyAttributeNone,
NULL);
/* If the greeter was started as a lock-screen, send message to our UI process. */
if (lightdm_greeter_get_lock_hint(greeter)) {
dom_document = webkit_web_page_get_dom_document(web_page);
dom_window = webkit_dom_document_get_default_view(dom_document);
if (dom_window) {
webkit_dom_dom_window_webkit_message_handlers_post_message(dom_window,
"GreeterBridge", message);
}
}
}