Skip to content

Commit 5f80a03

Browse files
committed
add __proto__ to HTMLElementBinding , for instanceof checking
1 parent f498872 commit 5f80a03

File tree

9 files changed

+71
-2
lines changed

9 files changed

+71
-2
lines changed

Source/Ejecta/EJAudio/EJBindingAudio.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ - (id)initWithContext:(JSContextRef)ctx argc:(size_t)argc argv:(const JSValueRef
2525
return self;
2626
}
2727

28+
- (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view {
29+
[super createWithJSObject:obj scriptView:view];
30+
31+
JSStringRef protoName = JSStringCreateWithUTF8CString("__proto__");
32+
JSObjectSetProperty(scriptView.jsGlobalContext, jsObject, protoName,
33+
scriptView->jsHTMLAudioElementProto, kJSPropertyAttributeReadOnly, NULL);
34+
}
35+
2836
- (void)dealloc {
2937
[loadCallback cancel];
3038
[loadCallback release];

Source/Ejecta/EJBindingGlobalUtils.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,17 @@ - (void)setAudioSession:(EJCoreAudioSession)session {
289289
}
290290
}
291291

292+
EJ_BIND_GET(__image__proto__, ctx ) {
293+
return scriptView->jsHTMLImageElementProto;
294+
}
295+
EJ_BIND_GET(__canvas__proto__, ctx ) {
296+
return scriptView->jsHTMLCanvasElementProto;
297+
}
298+
EJ_BIND_GET(__video__proto__, ctx ) {
299+
return scriptView->jsHTMLVideoElementProto;
300+
}
301+
EJ_BIND_GET(__audio__proto__, ctx ) {
302+
return scriptView->jsHTMLAudioElementProto;
303+
}
304+
292305
@end

Source/Ejecta/EJBindingVideo.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ - (id)initWithContext:(JSContextRef)ctx argc:(size_t)argc argv:(const JSValueRef
1212
return self;
1313
}
1414

15+
- (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view {
16+
[super createWithJSObject:obj scriptView:view];
17+
18+
JSStringRef protoName = JSStringCreateWithUTF8CString("__proto__");
19+
JSObjectSetProperty(scriptView.jsGlobalContext, jsObject, protoName,
20+
scriptView->jsHTMLVideoElementProto, kJSPropertyAttributeReadOnly, NULL);
21+
}
22+
1523
- (void)prepareGarbageCollection {
1624
[NSNotificationCenter.defaultCenter removeObserver:self];
1725
}

Source/Ejecta/EJCanvas/EJBindingCanvas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// and `style`, until getContext() is called. A Canvas can either host a 2D
66
// or WebGL Context.
77

8-
#import "EJBindingBase.h"
8+
#import "EJBindingEventedBase.h"
99
#import "EJTexture.h"
1010
#import "EJDrawable.h"
1111
#import "EJCanvasContext.h"
@@ -29,7 +29,7 @@ typedef enum {
2929
kEJCanvasImageRenderingPixelated
3030
} EJCanvasImageRendering;
3131

32-
@interface EJBindingCanvas : EJBindingBase <EJDrawable> {
32+
@interface EJBindingCanvas : EJBindingEventedBase <EJDrawable> {
3333
JSObjectRef jsCanvasContext;
3434
EJCanvasContext *renderingContext;
3535
EJCanvasContextMode contextMode;

Source/Ejecta/EJCanvas/EJBindingCanvas.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ @implementation EJBindingCanvas
1818
- (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view {
1919
[super createWithJSObject:obj scriptView:view];
2020

21+
JSStringRef protoName = JSStringCreateWithUTF8CString("__proto__");
22+
JSObjectSetProperty(scriptView.jsGlobalContext, jsObject, protoName,
23+
scriptView->jsHTMLCanvasElementProto, kJSPropertyAttributeReadOnly, NULL);
24+
2125
// If we don't have a screen canvas yet, make it this one
2226
if( !scriptView.hasScreenCanvas ) {
2327
isScreenCanvas = YES;

Source/Ejecta/EJCanvas/EJBindingImage.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
@implementation EJBindingImage
77
@synthesize texture;
88

9+
- (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view {
10+
[super createWithJSObject:obj scriptView:view];
11+
12+
JSStringRef protoName = JSStringCreateWithUTF8CString("__proto__");
13+
JSObjectSetProperty(scriptView.jsGlobalContext, jsObject, protoName,
14+
scriptView->jsHTMLImageElementProto, kJSPropertyAttributeReadOnly, NULL);
15+
}
16+
917
- (void)beginLoad {
1018
// This will begin loading the texture in a background thread and will call the
1119
// JavaScript onload callback when done

Source/Ejecta/EJJavaScriptView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@
9292
@public JSValueRef jsNull;
9393
@public JSValueRef jsTrue;
9494
@public JSValueRef jsFalse;
95+
96+
@public JSValueRef jsHTMLImageElementProto;
97+
@public JSValueRef jsHTMLCanvasElementProto;
98+
@public JSValueRef jsHTMLVideoElementProto;
99+
@public JSValueRef jsHTMLAudioElementProto;
95100
}
96101

97102
@property (nonatomic, copy) NSString *appFolder;

Source/Ejecta/EJJavaScriptView.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ -(void)setupWithAppFolder:(NSString*)folder {
113113
JSValueProtect(jsGlobalContext, jsTrue);
114114
jsFalse = JSValueMakeBoolean(jsGlobalContext, false);
115115
JSValueProtect(jsGlobalContext, jsFalse);
116+
117+
118+
jsHTMLImageElementProto = NSObjectToJSValue(jsGlobalContext, @{ @"_type" : @"HTMLImageElement" });
119+
JSValueProtect(jsGlobalContext, jsHTMLImageElementProto);
120+
jsHTMLCanvasElementProto = NSObjectToJSValue(jsGlobalContext, @{ @"_type" : @"HTMLCanvasElement" });
121+
JSValueProtect(jsGlobalContext, jsHTMLCanvasElementProto);
122+
jsHTMLVideoElementProto = NSObjectToJSValue(jsGlobalContext, @{ @"_type" : @"HTMLVideoElement" });
123+
JSValueProtect(jsGlobalContext, jsHTMLVideoElementProto);
124+
jsHTMLAudioElementProto = NSObjectToJSValue(jsGlobalContext, @{ @"_type" : @"HTMLAudioElement" });
125+
JSValueProtect(jsGlobalContext, jsHTMLAudioElementProto);
116126

117127
// Attach all native class constructors to 'Ejecta'
118128
classLoader = [[EJClassLoader alloc] initWithScriptView:self name:@"Ejecta"];

Source/Ejecta/Ejecta.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,19 @@ HTMLElement.prototype.removeEventListener = function(event, method){
267267
}
268268
};
269269

270+
HTMLImageElement = function(){};
271+
HTMLImageElement.prototype = ejecta.__image__proto__;
272+
273+
HTMLCanvasElement = function(){};
274+
HTMLCanvasElement.prototype = ejecta.__canvas__proto__;
275+
276+
HTMLVideoElement = function(){};
277+
HTMLVideoElement.prototype = ejecta.__video__proto__;
278+
279+
HTMLAudioElement = function(){};
280+
HTMLAudioElement.prototype = ejecta.__audio__proto__;
281+
282+
270283
// The document object
271284
window.document = {
272285
readyState: 'complete',

0 commit comments

Comments
 (0)