void Dollar::jsLoadTrace(const v8::FunctionCallbackInfo<Value>& args) { TryCatchCpp(args, [&args]() -> Local<Value> { if (args.Length() < 1) { ThrowTypeError("expected $.loadTrace(name, start(Mb), stop(Mb), onComplete)\r\n"); } v8::String::Utf8Value str(args[0]); int posStart = 0, posEnd = -1; if (args.Length() >= 2 && args[1]->IsInt32()) { posStart = args[1]->Int32Value(); } if (args.Length() >= 3 && args[2]->IsInt32()) { posEnd = args[2]->Int32Value(); } GetCurrentHost()->LoadTrace(*str, posStart, posEnd); return Local<Value>(); }); }
void Dollar::jsOnLoaded(const v8::FunctionCallbackInfo<Value> &args) { if (args.Length() != 1) { ThrowTypeError("use $.onLoaded(function)"); } Dollar * pThis = UnwrapThis<Dollar>(args.This()); pThis->_OnLoaded.Reset(Isolate::GetCurrent(), args[0].As<Function>()); }
void Dollar::jsImport(const FunctionCallbackInfo<Value>& args) { if(args.Length() != 1) { ThrowTypeError("$.import requires one parameter\r\n"); } v8::String::Utf8Value str(args[0]); args.GetReturnValue().Set(ImportWorker(*str, false)); }
static void KStackReturnTypeCheck(KonohaContext *kctx, KonohaStack *sfp, kMethod *mtd, KClass *reqType, KClass *thisClass) { if(!kMethod_Is(SmartReturn, mtd)) { KClass *returnType = kMethod_GetReturnType(mtd); returnType = returnType->realtype(kctx, returnType, thisClass); if(reqType == returnType || returnType->isSubType(kctx, returnType, reqType)) { if(KClass_Is(UnboxType, returnType) && !KClass_Is(UnboxType, reqType)) { KReturn(KLIB new_kObject(kctx, OnStack, returnType, sfp[K_RTNIDX].unboxValue)); } } else { ThrowTypeError(kctx, sfp, -1); } } }
static void KStackDynamicTypeCheck(KonohaContext *kctx, KonohaStack *sfp, kMethod *mtd, KClass *thisClass) { kushort_t i; kParam *pa = kMethod_GetParam(mtd); for(i = 0; i < pa->psize; i++) { KClass *objectType = kObject_class(sfp[i+1].asObject); KClass *paramType = KClass_(pa->paramtypeItems[i].attrTypeId); paramType = paramType->realtype(kctx, paramType, thisClass); if(objectType == paramType || objectType->isSubType(kctx, objectType, paramType)) { if(KClass_Is(UnboxType, paramType)) { KStackSetUnboxValue(sfp[i+1].unboxValue, kObject_Unbox(sfp[i+1].asObject)); } continue; // OK } ThrowTypeError(kctx, sfp, i + 1); } }
bool Dollar::ImportWorker(const char * pszName, bool opt) { std::fstream stm; TryCatch try_catch; // search file in different locations if(!OpenScriptStream(pszName, stm)) { if (opt) { return false; } else { std::string s = std::string("$.import cannot open file ") + pszName + "\r\n"; ThrowTypeError(s.c_str()); } } std::string scriptLine((std::istreambuf_iterator<char>(stm)), std::istreambuf_iterator<char>()); auto scriptSource = v8::String::NewFromUtf8(Isolate::GetCurrent(), scriptLine.c_str()); auto scriptName = v8::String::NewFromUtf8(Isolate::GetCurrent(), pszName); auto script = v8::Script::Compile(scriptSource, scriptName); if (script.IsEmpty()) { GetCurrentHost()->ReportException(Isolate::GetCurrent(), try_catch); return false; } auto v = script->Run(); if(v.IsEmpty()) { GetCurrentHost()->ReportException(Isolate::GetCurrent(), try_catch); return false; } return true; }
void Dollar::jsPost(const v8::FunctionCallbackInfo<Value>& args) { if (args.Length() != 1) { ThrowTypeError("use $.post(function)"); } Dollar * pThis = UnwrapThis<Dollar>(args.This()); auto sharedFunc = std::make_shared<v8::UniquePersistent<v8::Function>>(Isolate::GetCurrent(), args[0].As<Function>()); static_cast<JsHost*>(GetCurrentHost())->QueueInput([sharedFunc](v8::Isolate* iso) { auto localFunc = Local<Function>::New(iso, *sharedFunc); TryCatch try_catch; localFunc->Call(iso->GetCurrentContext()->Global(), 0, nullptr); if (try_catch.HasCaught()) { GetCurrentHost()->ReportException(v8::Isolate::GetCurrent(), try_catch); } }); }
void ThrowInvalidOpenCollection() { ThrowTypeError("expected $.openCollection(collection)\r\n"); }