@@ -12,6 +12,9 @@ nx_app_t *nx_get_app(JSContext *ctx, JSValueConst obj) {
12
12
static void finalizer_app (JSRuntime * rt , JSValue val ) {
13
13
nx_app_t * app = JS_GetOpaque (val , nx_app_class_id );
14
14
if (app ) {
15
+ if (app -> icon ) {
16
+ js_free_rt (rt , app -> icon );
17
+ }
15
18
js_free_rt (rt , app );
16
19
}
17
20
}
@@ -84,11 +87,16 @@ static JSValue nx_ns_app_new(JSContext *ctx, JSValueConst this_val, int argc,
84
87
85
88
// Seek to the icon section offset and read the icon data
86
89
fseek (file , asset_header_offset + icon_section_offset , SEEK_SET );
87
- fread (& data -> data .icon , icon_section_size , 1 , file );
90
+ data -> icon = js_malloc (ctx , icon_section_size );
91
+ if (!data -> icon ) {
92
+ fclose (file );
93
+ return JS_EXCEPTION ;
94
+ }
95
+ fread (data -> icon , icon_section_size , 1 , file );
88
96
89
97
// Seek to the nacp section offset and read the nacp data
90
98
fseek (file , asset_header_offset + nacp_section_offset , SEEK_SET );
91
- fread (& data -> data . nacp , nacp_section_size , 1 , file );
99
+ fread (& data -> nacp , nacp_section_size , 1 , file );
92
100
93
101
free (asset_header );
94
102
fclose (file );
@@ -106,24 +114,35 @@ static JSValue nx_ns_app_new(JSContext *ctx, JSValueConst this_val, int argc,
106
114
u32 icon_section_size = * (u32 * )(asset_header + 0x10 );
107
115
u32 nacp_section_offset = * (u32 * )(asset_header + 0x18 );
108
116
u32 nacp_section_size = * (u32 * )(asset_header + 0x20 );
109
- memcpy (& data -> data .icon , asset_header + icon_section_offset ,
117
+ data -> icon = js_mallocz (ctx , icon_section_size );
118
+ if (!data -> icon ) {
119
+ return JS_EXCEPTION ;
120
+ }
121
+ memcpy (data -> icon , asset_header + icon_section_offset ,
110
122
icon_section_size );
111
- memcpy (& data -> data . nacp , asset_header + nacp_section_offset ,
123
+ memcpy (& data -> nacp , asset_header + nacp_section_offset ,
112
124
nacp_section_size );
113
125
data -> icon_size = icon_section_size ;
114
126
loaded = true;
115
127
}
116
128
117
129
if (!loaded ) {
118
130
size_t outSize ;
131
+ NsApplicationControlData buf ;
119
132
Result rc = nsGetApplicationControlData (
120
- NsApplicationControlSource_Storage , application_id , & data -> data ,
121
- sizeof (data -> data ), & outSize );
133
+ NsApplicationControlSource_Storage , application_id , & buf ,
134
+ sizeof (NsApplicationControlData ), & outSize );
122
135
if (R_FAILED (rc )) {
123
136
return nx_throw_libnx_error (ctx , rc ,
124
137
"nsGetApplicationControlData()" );
125
138
}
126
- data -> icon_size = outSize > 0 ? outSize - sizeof (data -> data .nacp ) : 0 ;
139
+ data -> icon_size = outSize > 0 ? outSize - sizeof (buf .nacp ) : 0 ;
140
+ data -> icon = js_malloc (ctx , data -> icon_size );
141
+ if (!data -> icon ) {
142
+ return JS_EXCEPTION ;
143
+ }
144
+ memcpy (data -> icon , & buf .icon , data -> icon_size );
145
+ memcpy (& data -> nacp , & buf .nacp , sizeof (NacpStruct ));
127
146
}
128
147
129
148
JSValue app = JS_NewObjectClass (ctx , nx_app_class_id );
@@ -137,7 +156,7 @@ static JSValue nx_ns_app_id(JSContext *ctx, JSValueConst this_val, int argc,
137
156
if (!app ) {
138
157
return JS_EXCEPTION ;
139
158
}
140
- return JS_NewBigUint64 (ctx , app -> data . nacp .presence_group_id );
159
+ return JS_NewBigUint64 (ctx , app -> nacp .presence_group_id );
141
160
}
142
161
143
162
static JSValue nx_ns_app_nacp (JSContext * ctx , JSValueConst this_val , int argc ,
@@ -146,8 +165,7 @@ static JSValue nx_ns_app_nacp(JSContext *ctx, JSValueConst this_val, int argc,
146
165
if (!app ) {
147
166
return JS_EXCEPTION ;
148
167
}
149
- return JS_NewArrayBufferCopy (ctx , (uint8_t * )& app -> data .nacp ,
150
- sizeof (app -> data .nacp ));
168
+ return JS_NewArrayBufferCopy (ctx , (uint8_t * )& app -> nacp , sizeof (app -> nacp ));
151
169
}
152
170
153
171
static JSValue nx_ns_app_icon (JSContext * ctx , JSValueConst this_val , int argc ,
@@ -159,8 +177,7 @@ static JSValue nx_ns_app_icon(JSContext *ctx, JSValueConst this_val, int argc,
159
177
if (app -> icon_size <= 0 ) {
160
178
return JS_UNDEFINED ;
161
179
}
162
- return JS_NewArrayBufferCopy (ctx , (uint8_t * )& app -> data .icon ,
163
- app -> icon_size );
180
+ return JS_NewArrayBufferCopy (ctx , (uint8_t * )app -> icon , app -> icon_size );
164
181
}
165
182
166
183
static JSValue nx_ns_app_name (JSContext * ctx , JSValueConst this_val , int argc ,
@@ -170,7 +187,7 @@ static JSValue nx_ns_app_name(JSContext *ctx, JSValueConst this_val, int argc,
170
187
return JS_EXCEPTION ;
171
188
}
172
189
NacpLanguageEntry * langEntry ;
173
- Result rc = nacpGetLanguageEntry (& app -> data . nacp , & langEntry );
190
+ Result rc = nacpGetLanguageEntry (& app -> nacp , & langEntry );
174
191
if (R_FAILED (rc )) {
175
192
return nx_throw_libnx_error (ctx , rc , "nacpGetLanguageEntry()" );
176
193
}
@@ -188,7 +205,7 @@ static JSValue nx_ns_app_author(JSContext *ctx, JSValueConst this_val, int argc,
188
205
return JS_EXCEPTION ;
189
206
}
190
207
NacpLanguageEntry * langEntry ;
191
- Result rc = nacpGetLanguageEntry (& app -> data . nacp , & langEntry );
208
+ Result rc = nacpGetLanguageEntry (& app -> nacp , & langEntry );
192
209
if (R_FAILED (rc )) {
193
210
return nx_throw_libnx_error (ctx , rc , "nacpGetLanguageEntry()" );
194
211
}
@@ -227,7 +244,7 @@ static JSValue nx_ns_app_version(JSContext *ctx, JSValueConst this_val,
227
244
if (!app ) {
228
245
return JS_EXCEPTION ;
229
246
}
230
- return JS_NewString (ctx , app -> data . nacp .display_version );
247
+ return JS_NewString (ctx , app -> nacp .display_version );
231
248
}
232
249
233
250
static JSValue nx_ns_app_init (JSContext * ctx , JSValueConst this_val , int argc ,
0 commit comments