Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 78a07ac

Browse files
committed
Add some additional error handling
1 parent 720ea83 commit 78a07ac

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

javascript.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ hjs_script_getproperty (JSContext* context, string property, string fallback)
277277
char* cstr;
278278
string str;
279279

280-
if (JS_HasProperty (context, globals, property.c_str(), &found))
280+
if (context != nullptr
281+
&& JS_HasProperty (context, globals, property.c_str(), &found))
281282
{
282283
if (found)
283284
{
@@ -1427,23 +1428,25 @@ js_init (JSContext **cx, JSRuntime **rt, JSObject **globals, bool fake)
14271428
static void
14281429
js_deinit (JSContext *cx, JSRuntime *rt)
14291430
{
1430-
JS_DestroyContext(cx);
1431-
JS_DestroyRuntime(rt);
1431+
if (cx != nullptr)
1432+
JS_DestroyContext(cx);
1433+
if (rt != nullptr)
1434+
JS_DestroyRuntime(rt);
14321435
}
14331436

14341437
js_script::js_script (string file, string src)
14351438
{
1436-
JSObject* fake_globals;
1437-
JSContext* fake_context;
1438-
JSRuntime* fake_runtime;
1439+
JSObject* fake_globals = nullptr;
1440+
JSContext* fake_context = nullptr;
1441+
JSRuntime* fake_runtime = nullptr;
14391442

14401443
js_script_list.push_back(this);
14411444

14421445
filename = file;
14431446

14441447
// create a fake runtime to get the scripts name without actually running it, is there an easier way?
1445-
js_init (&fake_context, &fake_runtime, &fake_globals, true);
1446-
JS_EvaluateScript (fake_context, fake_globals, src.c_str(), src.length(), file.c_str(), 0, nullptr);
1448+
if (js_init (&fake_context, &fake_runtime, &fake_globals, true))
1449+
JS_EvaluateScript (fake_context, fake_globals, src.c_str(), src.length(), file.c_str(), 0, nullptr);
14471450

14481451
name = hjs_script_getproperty (fake_context, "SCRIPT_NAME", hjs_util_shrinkfile(file));
14491452
desc = hjs_script_getproperty (fake_context, "SCRIPT_DESC", file);
@@ -1453,8 +1456,10 @@ js_script::js_script (string file, string src)
14531456
js_deinit (fake_context, fake_runtime);
14541457

14551458
// now the real thing..
1456-
js_init (&context, &runtime, &globals, false);
1457-
JS_EvaluateScript (context, globals, src.c_str(), src.length(), file.c_str(), 0, nullptr);
1459+
if (js_init (&context, &runtime, &globals, false))
1460+
JS_EvaluateScript (context, globals, src.c_str(), src.length(), file.c_str(), 0, nullptr);
1461+
else
1462+
hexchat_printf (ph, "\00320JavaScript Error:\017: Failed to initialize %s", name.c_str());
14581463
}
14591464

14601465
void

0 commit comments

Comments
 (0)