-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathenv_quickjs.cc
More file actions
65 lines (60 loc) · 1.85 KB
/
env_quickjs.cc
File metadata and controls
65 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <napi/env.h>
#include "js_native_api_quickjs.h"
#include <quickjs.h>
namespace Napi
{
Env Attach(JSContext* context)
{
napi_env env_ptr{new napi_env__};
env_ptr->context = context;
env_ptr->current_context = env_ptr->context;
// Get Object.prototype.hasOwnProperty
JSValue global = JS_GetGlobalObject(context);
JSValue object = JS_GetPropertyStr(context, global, "Object");
if (!JS_IsException(object) && JS_IsObject(object))
{
JSValue prototype = JS_GetPrototype(context, object);
if (!JS_IsException(prototype) && JS_IsObject(prototype))
{
JSValue hasOwnProperty = JS_GetPropertyStr(context, prototype, "hasOwnProperty");
if (!JS_IsException(hasOwnProperty))
{
env_ptr->has_own_property_function = hasOwnProperty;
}
else
{
JS_FreeValue(context, hasOwnProperty);
}
JS_FreeValue(context, prototype);
}
else if (JS_IsException(prototype))
{
JS_FreeValue(context, prototype);
}
JS_FreeValue(context, object);
}
else if (JS_IsException(object))
{
JS_FreeValue(context, object);
}
JS_FreeValue(context, global);
return {env_ptr};
}
void Detach(Env env)
{
napi_env env_ptr{env};
if (env_ptr)
{
if (!JS_IsUndefined(env_ptr->has_own_property_function))
{
JS_FreeValue(env_ptr->context, env_ptr->has_own_property_function);
}
delete env_ptr;
}
}
JSContext* GetContext(Env env)
{
napi_env env_ptr{env};
return env_ptr->context;
}
}