<s> """<STR_LIT>""" <EOL> import py , sys <EOL> from pypy . rpython . lltypesystem import lltype , rffi <EOL> from pypy . translator . tool . cbuild import ExternalCompilationInfo , log <EOL> from pypy . rpython . tool import rffi_platform as platform <EOL> class WebkitNotFound ( Exception ) : <EOL> pass <EOL> def error ( ) : <EOL> print __doc__ <EOL> raise WebkitNotFound ( ) <EOL> webkitdir = py . magic . autopath ( ) . dirpath ( ) . join ( '<STR_LIT>' ) <EOL> if not webkitdir . check ( dir = True ) : <EOL> error ( ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> libdir = webkitdir / '<STR_LIT>' / '<STR_LIT>' / '<STR_LIT>' <EOL> if not libdir . join ( '<STR_LIT>' ) . check ( ) : <EOL> error ( ) <EOL> library = [ '<STR_LIT>' ] <EOL> framework = [ ] <EOL> elif sys . platform == '<STR_LIT>' : <EOL> libdir = webkitdir / '<STR_LIT>' / '<STR_LIT>' <EOL> if not libdir . join ( '<STR_LIT>' ) . check ( ) : <EOL> error ( ) <EOL> framework = [ '<STR_LIT>' ] <EOL> library = [ ] <EOL> include_dirs = [ webkitdir , <EOL> webkitdir / '<STR_LIT>' / '<STR_LIT>' ] <EOL> eci = ExternalCompilationInfo ( <EOL> libraries = library , <EOL> library_dirs = [ str ( libdir ) ] , <EOL> frameworks = framework , <EOL> include_dirs = include_dirs , <EOL> includes = [ '<STR_LIT>' ] <EOL> ) <EOL> def external ( name , args , result , ** kwds ) : <EOL> return rffi . llexternal ( name , args , result , compilation_info = eci , ** kwds ) <EOL> def copaqueptr ( name ) : <EOL> return rffi . COpaquePtr ( name , compilation_info = eci ) <EOL> JSContextRef = rffi . VOIDP <EOL> JSStringRef = rffi . VOIDP <EOL> JSObjectRef = rffi . VOIDP <EOL> JSPropertyNameArrayRef = rffi . VOIDP <EOL> JSValueRef = rffi . VOIDP <EOL> JSValueRefP = rffi . CArrayPtr ( JSValueRef ) <EOL> class Configure : <EOL> _compilation_info_ = eci <EOL> JSType = platform . SimpleType ( '<STR_LIT>' , rffi . INT ) <EOL> JSPropertyAttributes = platform . SimpleType ( '<STR_LIT>' , rffi . INT ) <EOL> c_bool = platform . SimpleType ( '<STR_LIT:bool>' , rffi . INT ) <EOL> for name in [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> setattr ( Configure , name , platform . ConstantInteger ( name ) ) <EOL> globals ( ) . update ( platform . configure ( Configure ) ) <EOL> NULL = lltype . nullptr ( rffi . VOIDP . TO ) <EOL> _JSEvaluateScript = external ( '<STR_LIT>' , <EOL> [ JSContextRef , JSStringRef , <EOL> JSObjectRef , JSStringRef , <EOL> rffi . INT , JSValueRefP ] , <EOL> JSValueRef ) <EOL> class JSException ( Exception ) : <EOL> def __init__ ( self , context , jsexc ) : <EOL> self . context = context <EOL> self . jsexc = jsexc <EOL> def repr ( self ) : <EOL> v = JSValueToString ( self . context , self . jsexc ) <EOL> return JSStringGetUTF8CString ( v ) <EOL> __str__ = repr <EOL> def _can_raise_wrapper ( name , args , res , exc_class = JSException ) : <EOL> llf = external ( name , args + [ JSValueRefP ] , res ) <EOL> def f ( context , * a ) : <EOL> exc_data = lltype . malloc ( JSValueRefP . TO , <NUM_LIT:1> , flavor = '<STR_LIT>' , zero = True ) <EOL> res = llf ( context , * a + ( exc_data , ) ) <EOL> exc = exc_data [ <NUM_LIT:0> ] <EOL> lltype . free ( exc_data , flavor = '<STR_LIT>' ) <EOL> if exc : <EOL> raise exc_class ( context , exc ) <EOL> return res <EOL> f . func_name = name <EOL> return f <EOL> def JSEvaluateScript ( ctx , source , this ) : <EOL> exc_data = lltype . malloc ( JSValueRefP . TO , <NUM_LIT:1> , flavor = '<STR_LIT>' ) <EOL> sourceref = JSStringCreateWithUTF8CString ( source ) <EOL> res = _JSEvaluateScript ( ctx , sourceref , this , NULL , <NUM_LIT:0> , exc_data ) <EOL> exc = exc_data [ <NUM_LIT:0> ] <EOL> lltype . free ( exc_data , flavor = '<STR_LIT>' ) <EOL> if exc : <EOL> return res <EOL> raise JSException ( ctx , exc ) <EOL> def empty_object ( ctx ) : <EOL> return JSEvaluateScript ( ctx , '<STR_LIT>' , NULL ) <EOL> JSGarbageCollect = external ( '<STR_LIT>' , [ JSContextRef ] , lltype . Void ) <EOL> JSGlobalContextRelease = external ( '<STR_LIT>' , [ JSContextRef ] , <EOL> lltype . Void ) <EOL> _JSGlobalContextCreate = external ( '<STR_LIT>' , [ rffi . VOIDP ] , <EOL> JSContextRef ) <EOL> def JSGlobalContextCreate ( ) : <EOL> return _JSGlobalContextCreate ( lltype . nullptr ( rffi . VOIDP . TO ) ) <EOL> JSContextGetGlobalObject = external ( '<STR_LIT>' , [ JSContextRef ] , <EOL> JSObjectRef ) <EOL> JSStringCreateWithUTF8CString = external ( '<STR_LIT>' , <EOL> [ rffi . CCHARP ] , JSStringRef ) <EOL> JSStringGetLength = external ( '<STR_LIT>' , [ JSStringRef ] , rffi . INT ) <EOL> _JSStringGetUTF8CString = external ( '<STR_LIT>' , <EOL> [ JSStringRef , rffi . CCHARP , rffi . INT ] , <EOL> rffi . INT ) <EOL> _JSStringGetMaximumUTF8CStringSize = external ( <EOL> '<STR_LIT>' , [ JSStringRef ] , rffi . INT ) <EOL> def JSStringGetUTF8CString ( s ) : <EOL> lgt = _JSStringGetMaximumUTF8CStringSize ( s ) <EOL> buf = lltype . malloc ( rffi . CCHARP . TO , lgt , flavor = '<STR_LIT>' ) <EOL> newlgt = _JSStringGetUTF8CString ( s , buf , lgt ) <EOL> res = rffi . charp2strn ( buf , newlgt ) <EOL> lltype . free ( buf , flavor = '<STR_LIT>' ) <EOL> return res <EOL> JSValueMakeString = external ( '<STR_LIT>' , [ JSContextRef , JSStringRef ] , <EOL> JSValueRef ) <EOL> JSValueMakeNumber = external ( '<STR_LIT>' , [ JSContextRef , rffi . DOUBLE ] , <EOL> JSValueRef ) <EOL> JSValueMakeBoolean = external ( '<STR_LIT>' , [ JSContextRef , c_bool ] , <EOL> JSValueRef ) <EOL> JSValueToBoolean = external ( '<STR_LIT>' , [ JSContextRef , JSValueRef ] , <EOL> c_bool ) <EOL> JSValueMakeUndefined = external ( '<STR_LIT>' , [ JSContextRef ] , <EOL> JSValueRef ) <EOL> JSValueGetType = external ( '<STR_LIT>' , [ JSContextRef , JSValueRef ] , <EOL> JSType ) <EOL> JSValueProtect = external ( '<STR_LIT>' , [ JSContextRef , JSValueRef ] , <EOL> lltype . Void ) <EOL> JSValueUnprotect = external ( '<STR_LIT>' , [ JSContextRef , JSValueRef ] , <EOL> lltype . Void ) <EOL> JSValueToString = _can_raise_wrapper ( '<STR_LIT>' , <EOL> [ JSContextRef , JSValueRef ] , <EOL> JSStringRef ) <EOL> JSObjectCopyPropertyNames = external ( '<STR_LIT>' , <EOL> [ JSContextRef , JSObjectRef ] , <EOL> JSPropertyNameArrayRef ) <EOL> JSPropertyNameArrayGetCount = external ( '<STR_LIT>' , <EOL> [ JSPropertyNameArrayRef ] , <EOL> rffi . INT ) <EOL> JSPropertyNameArrayGetNameAtIndex = external ( <EOL> '<STR_LIT>' , <EOL> [ JSPropertyNameArrayRef , rffi . INT ] , <EOL> JSStringRef ) <EOL> def JSPropertyList ( ctx , obj ) : <EOL> propnames = JSObjectCopyPropertyNames ( ctx , obj ) <EOL> count = JSPropertyNameArrayGetCount ( propnames ) <EOL> res = [ ] <EOL> for i in range ( count ) : <EOL> js_str = JSPropertyNameArrayGetNameAtIndex ( propnames , i ) <EOL> s = JSStringGetUTF8CString ( js_str ) <EOL> res . append ( s ) <EOL> return res <EOL> JSObjectHasProperty = external ( '<STR_LIT>' , <EOL> [ JSContextRef , JSObjectRef , JSStringRef ] , <EOL> lltype . Bool ) <EOL> JSObjectGetProperty = _can_raise_wrapper ( '<STR_LIT>' , <EOL> [ JSContextRef , <EOL> JSObjectRef , <EOL> JSStringRef ] , <EOL> JSValueRef ) <EOL> JSObjectSetProperty = _can_raise_wrapper ( '<STR_LIT>' , <EOL> [ JSContextRef , JSObjectRef , <EOL> JSStringRef , JSValueRef , <EOL> JSPropertyAttributes ] , <EOL> lltype . Void ) <EOL> _JSObjectCallAsFunction = external ( '<STR_LIT>' , <EOL> [ JSContextRef , JSObjectRef , <EOL> JSObjectRef , rffi . INT , <EOL> JSValueRefP , JSValueRefP ] , JSValueRef ) <EOL> def JSObjectCallAsFunction ( ctx , object , this , args ) : <EOL> ll_args = lltype . malloc ( JSValueRefP . TO , len ( args ) , flavor = '<STR_LIT>' ) <EOL> exc_data = lltype . malloc ( JSValueRefP . TO , <NUM_LIT:1> , flavor = '<STR_LIT>' , zero = True ) <EOL> for i in range ( len ( args ) ) : <EOL> ll_args [ i ] = args [ i ] <EOL> res = _JSObjectCallAsFunction ( ctx , object , this , len ( args ) , ll_args , <EOL> exc_data ) <EOL> lltype . free ( ll_args , flavor = '<STR_LIT>' ) <EOL> exc = exc_data [ <NUM_LIT:0> ] <EOL> lltype . free ( exc_data , flavor = '<STR_LIT>' ) <EOL> if exc : <EOL> raise JSException ( ctx , exc ) <EOL> return res <EOL> JSValueToNumber = _can_raise_wrapper ( '<STR_LIT>' , <EOL> [ JSContextRef , JSValueRef ] , <EOL> rffi . DOUBLE ) <EOL> _JSCallback = rffi . CCallback ( [ JSContextRef , JSObjectRef , JSObjectRef , rffi . INT , <EOL> JSValueRefP , JSValueRefP ] , JSValueRef ) <EOL> JSObjectMakeFunctionWithCallback = external ( <EOL> '<STR_LIT>' , [ JSContextRef , JSStringRef , <EOL> _JSCallback ] , JSObjectRef ) <EOL> def create_js_callback ( callable ) : <EOL> def js_callback ( ctx , function , this , argcount , ll_args , exc ) : <EOL> try : <EOL> args = [ ll_args [ i ] for i in range ( argcount ) ] <EOL> return callable ( ctx , function , this , args ) <EOL> except : <EOL> exc [ <NUM_LIT:0> ] = JSValueMakeString ( ctx , JSStringCreateWithUTF8CString ( <EOL> '<STR_LIT>' ) ) <EOL> return lltype . nullptr ( JSValueRef . TO ) <EOL> def factory ( ctx , name ) : <EOL> js_name = JSStringCreateWithUTF8CString ( name ) <EOL> return JSObjectMakeFunctionWithCallback ( ctx , js_name , js_callback ) <EOL> return factory </s>
