当前位置: 首页 > news >正文

设计一个官方网站推广广告/武汉百度推广代运营

设计一个官方网站推广广告,武汉百度推广代运营,做课件的软件下载带有蓝色的网站,万网服务器网站建设v8引擎为Google开发的JS语言解释器有着高效易用等特点,通常它执行一个Js脚本需要经过编译和运行步骤, 由于我们的脚本程序很可能不正确,随时造成过程环节的异常,我们来解决一个异常以及错误的捕捉和处理过程,如下脚本&…

v8引擎为Google开发的JS语言解释器有着高效易用等特点,通常它执行一个Js脚本需要经过编译和运行步骤,

由于我们的脚本程序很可能不正确,随时造成过程环节的异常,我们来解决一个异常以及错误的捕捉和处理过程,如下脚本:

test.js:

var str = 'Hello';
str
= str + ', World!';
alert(str);

  

使用以下经典的V8 C++示例代码来执行脚本:

#include <v8.h>
using namespace v8;

//提取js文件
v8::Handle<v8::String> ReadFile(const char* name)
{
FILE
* file = fopen(name, "rb");
if (file == NULL) return v8::Handle<v8::String>();
fseek(file,
0, SEEK_END);
int size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size]
= '\0';
for (int i = 0; i < size;)
{
int read = fread(&chars[i], 1, size - i, file);
i
+= read;
}
fclose(file);
v8::Handle
<v8::String> result = v8::String::New(chars, size);
delete[] chars;
return result;
}

int main()
{
HandleScope handle_scope;
Handle
<Context> context = Context::New();
Context::Scope context_scope(context);

const char * fname = "test.js";
Handle
<String> source = ReadFile(fname);
//ExecuteString(source, String::New(fname), true, true);

Handle
<Script> script = Script::Compile(source);
Handle
<Value> result = script->Run();
String::AsciiValue ascii(result);
printf(
"%s\n", *ascii);
return 0;
}

 

然后编译程序 

# g++ -o test test.cpp -lv8 

# ./test

<unknown>:58: Uncaught ReferenceError: alert is not defined

Segmentation fault

在执行过程遇到JS语法错误时,V8很干脆地中上了进程,提醒段错误。

在这我们正常应用环境是无法使用的,需要有一种异常处理机制来收集并处理错误。

这就是v8::TryCatch ,在编译前声明,在之后的编译和执行中只要有错误的发生均可捕获。

查找了相关的V8源代码,改进后的代码实现如下(函数体),

其中source为源文件内容,name用来标记当前脚本名称(文件名),

print_result表示是否打印脚本执行结果,report_exceptions表示是否报告异常(错误):

 

bool ExecuteString(v8::Handle<v8::String> source,
v8::Handle
<v8::Value> name,
bool print_result,
bool report_exceptions)
{
v8::HandleScope handle_scope;
v8::TryCatch try_catch;
//这里设置异常机制
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
if (script.IsEmpty()) {
// Print errors that happened during compilation.
if (report_exceptions)
ReportException(
&try_catch);
return false;
}
else {
v8::Handle
<v8::Value> result = script->Run();
if (result.IsEmpty()) {
assert(try_catch.HasCaught());
// Print errors that happened during execution.
if (report_exceptions)
ReportException(
&try_catch);
return false;
}
else {
assert(
!try_catch.HasCaught());
if (print_result && !result->IsUndefined()) {
// If all went well and the result wasn't undefined then print
// the returned value.
v8::String::Utf8Value str(result);
const char* cstr = ToCString(str);
printf(
"%s\n", cstr);
}
return true;
}
}
}

 

相关的异常处理函数:

 

const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}


void ReportException(v8::TryCatch* try_catch) {
v8::HandleScope handle_scope;
v8::String::Utf8Value exception(try_catch
->Exception());
const char* exception_string = ToCString(exception);
v8::Handle
<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
printf("%s\n", exception_string);
}
else {
// Print (filename):(line number): (message).
v8::String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
printf(
"%s:%i: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
v8::String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = ToCString(sourceline);
printf(
"%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
printf(
" ");
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
printf(
"^");
}
printf(
"\n");
v8::String::Utf8Value stack_trace(try_catch
->StackTrace());
if (stack_trace.length() > 0) {
const char* stack_trace_string = ToCString(stack_trace);
printf(
"%s\n", stack_trace_string);
}
}
}

 

最后修改main入口:
int main(int argc, char* argv[])
{
HandleScope handle_scope;

Handle
<Context> context = Context::New();
Context::Scope context_scope(context);

const char * fname = "test.js";
Handle
<String> source = ReadFile(fname);
ExecuteString(source, String::New(fname),
true, true);

return 0;
}

  

重新编译后运行结果为:

# ./test

test.js:3: ReferenceError: alert is not defined

alert(str);

^

ReferenceError: alert is not defined

    at test.js:3:1

已经可以正常报告错误。

转载于:https://www.cnblogs.com/lajabs/archive/2011/08/10/2133780.html

http://www.lbrq.cn/news/1328113.html

相关文章:

  • 国网典型设计最新版/seo顾问服务四川
  • wordpress 做网站/天津关键词排名推广
  • 网站建设都包括哪些方面/如何进行搜索引擎营销
  • 做网站编辑/ios aso优化工具
  • 建设行业个人云网站/seo外包服务公司
  • 鞍山做网站排名/青岛百度seo代理
  • 网络推广方法技巧/seo排名优化软件有用
  • 微信答题小程序怎么做/天津百度seo推广
  • 快速网站建设费用/专业全网优化
  • 安卓盒子 做网站/互联网营销师证书含金量
  • 盐山做网站的/网络推广工作
  • 1元涨1000粉丝网站/seo名词解释
  • app与网站的关系/上海网站seo
  • 广西两学一做考试网站/网址之家
  • phpcms 网站模板/seo教育培训机构
  • 阿里云备案个人可以做网站吗/保定百度首页优化
  • 用网站做简历/加强服务保障满足群众急需ruu7
  • 网站建设联/湖南靠谱关键词优化
  • 可以做哪些网站/搜狗竞价
  • 大学生简历免费制作网站/推广引流平台app大全
  • 在那些网站做宣传更好/厦门人才网唯一官网
  • 万网的网站建设/天津关键词优化网排名
  • 北京疫情宣布解除时间/郑州靠谱seo整站优化
  • 网站建设 模块/惠州百度seo在哪
  • 游戏推广平台代理加盟/六年级上册数学优化设计答案
  • 个体搞网站建设 经营范围/独立站谷歌seo
  • 做网站的用途/企业微信会话存档
  • 免费手机图片编辑器/seo每日一贴
  • 做一个网站后期维护需要做什么/海南seo顾问服务
  • 做网站需要企业/网站文章优化技巧
  • 京东云轻量云服务器与腾讯云域名结合配置网站及申请SSL证书流程详解
  • 如何在 Mac OS 上安装 Cursor
  • Apache RocketMQ中 Consumer Group(消费者组)的详细说明
  • STM32——HAL 库MDK工程创建
  • 基于 Amazon Nova Sonic 和 MCP 构建语音交互 Agent
  • LLMs之Agent:GLM-4.5的简介、安装和使用方法、案例应用之详细攻略