
    ]i:                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZ ej                    Z		 d dl
Z
n# e$ r dZ
Y nw xY wd Zd Z ed          Zeej                            e          ndZej        d          dk     reZn
d dlmZmZ eZ G d de          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de!          Z" G d de!          Z# G d de!          Z$d Z%d Z& G d de!          Z'e'Z( G d d e!          Z) G d! d"e j*                  Z+ G d# d$e j*                  Z, G d% d&e!          Z-dS )'    Nc                  `    t          j        d          } | |                     d          rdS dS )z+Return the libc name of the current system.HOST_GNU_TYPENmuslmuslcglibc)	sysconfigget_config_varendswith)targets    F/usr/local/lib/python3.11/dist-packages/py_mini_racer/py_mini_racer.py_get_libc_namer      s3    %o66Ffoof55w7    c                    t           j        dk    r!t          j        dk    rt          dk    rd\  }}npt          dk    rd\  }}n_t           j        dk    rt          j        dk    rd\  }}n9t          j        dk    rd	\  }}n#d
d                    t                                }}d}t          t          dd          }|&t           j        	                    ||| z   |z             }|"t          t          j        d|| z   |z             }|gt           j                            t           j                            t                              }t           j        	                    ||| z   |z             }|S )u   
    Return the path of the library called `name` on the current system.
    主要修改这个函数，用来获取不同系统下的动态链接库
    posixdarwinarm64)armlib.dylibaarch64)r   z	.glibc.so)libr   win32) z.dllr   z.{}.soN_MEIPASSpy_mini_racer)osnamesysplatformsys_arcformatr   getattrpathjoinpkg_resourcesresource_filenamedirnameabspath__file__)r   prefixextfnmeipassroot_dirs         r   _get_lib_pathr.      sG   
 
w'clh667g;M;M(	I		+	G		 8 8%		 	  X__^-=-=>>	Bc:t,,GW\\'6D=3#677	zm/,_ftmc>QRR	z7??27??8#<#<==W\\(FTMC$788Ir   
mini_racer   )AnyOptionalc                       e Zd ZdZdS )MiniRacerBaseExceptionzBase MiniRacer exception.N__name__
__module____qualname____doc__ r   r   r4   r4   C   s        ####r   r4   c                       e Zd ZdZdS )JSParseExceptionzJavaScript could not be parsed.Nr5   r:   r   r   r<   r<   G           ))))r   r<   c                       e Zd ZdZdS )JSEvalExceptionz!JavaScript could not be executed.Nr5   r:   r   r   r?   r?   K   s        ++++r   r?   c                       e Zd ZdZdS )JSOOMExceptionz'JavaScript execution ran out of memory.Nr5   r:   r   r   rA   rA   O           1111r   rA   c                       e Zd ZdZdS )JSTimeoutExceptionzJavaScript execution timed out.Nr5   r:   r   r   rD   rD   S   r=   r   rD   c                       e Zd ZdZdS )JSConversionExceptionz'JavaScript type could not be converted.Nr5   r:   r   r   rF   rF   W   rB   r   rF   c                       e Zd ZdZdS )WrongReturnTypeExceptionz0Invalid type returned by the JavaScript runtime.Nr5   r:   r   r   rH   rH   [   s        ::::r   rH   c                       e Zd ZdZd Zd ZdS )JSObjectzJavaScript object.c                     || _         d S Nid)selfrN   s     r   __init__zJSObject.__init__b   s    r   c                     | j         S rL   rM   rO   s    r   __hash__zJSObject.__hash__e   s	    wr   N)r6   r7   r8   r9   rP   rS   r:   r   r   rJ   rJ   _   s8              r   rJ   c                       e Zd ZdZdS )
JSFunctionzJavaScript function.Nr5   r:   r   r   rU   rU   i   s        r   rU   c                       e Zd ZdZdS )JSSymbolzJavaScript symbol.Nr5   r:   r   r   rW   rW   m   s        r   rW   c                 ,    t          | t                    S )u   Check if a value is a valid unicode string.

    >>> is_unicode(u'foo')
    True
    >>> is_unicode(u'✌')
    True
    >>> is_unicode(b'foo')
    False
    >>> is_unicode(42)
    False
    >>> is_unicode(('abc',))
    False
    )
isinstanceUNICODE_TYPE)values    r   
is_unicoder\   q   s     e\***r   c                  l   t           $t          j                            t                     s't	          d                    t                               t          j        t                     } t          j        g| j	        _
        t          j        | j	        _        t          j        t          j        t          j        t          j        t          j        g| j        _
        t          j        t$                    | j        _        t          j        t          j        g| j        _
        t          j        g| j        _
        t          j        g| j        _
        t          j        t$                    | j        _        t          j        g| j        _
        t          j        g| j        _
        t          j        t$                    | j        _        t          j        t          j        g| j        _
        d | j        _        t          j        g| j        _
        t          j        | j        _        t          j        | j        _        | S )Nz"Native library not available at {})EXTENSION_PATHr   r"   existsRuntimeErrorr    ctypesCDLLc_char_pmr_init_contextargtypesc_void_prestypec_intc_ulongc_size_tmr_eval_contextPOINTERMiniRacerValueStructmr_free_valuemr_free_contextmr_heap_statsmr_low_memory_notificationmr_heap_snapshotmr_set_soft_memory_limitmr_soft_memory_limit_reachedc_boolmr_v8_version)_ext_handles    r   _build_ext_handlerx      ss   RW^^N%C%C?FF~VVWWW+n--K,2O+<K(*0/K' 	,K( +1.9M*N*NK'*0/6?)KK&,2O+<K(*0/):K&(.7K(L(LK%7=6GK*3-3_,=K )+1>:N+O+OK (5;_fo4VK(137K(09?8IK,57=}K,4(.K%r   c                   |    e Zd ZdZeZdgZdZd Ze	d             Z
ddZddZd Zd	 Zd
 Zd Zd Zd Zd Zd ZdS )	MiniRacerz
    MiniRacer evaluates JavaScript code using a V8 isolate.

    :cvar json_impl: JSON module used by helper methods default is :py:mod:`json`
    :cvar v8_flags: Flags used for V8 initialization
    :vartype v8_flags: class attribute list of str
    z--single-threadedNc                    | j         j        t                      | j         _        | j                            d                    | j                                      d                    | _        t          j	                    | _
        d S )N utf-8)	__class__r*   rx   rd   r#   v8_flagsencodectx	threadingLocklockrR   s    r   rP   zMiniRacer.__init__   sc    >%!2!4!4DN8++CHHT],C,C,J,J7,S,STTN$$			r   c                 N    t          | j                                                  S )zReturn the V8 version string.)rZ   r*   rv   rR   s    r   
v8_versionzMiniRacer.v8_version   s      DH2244555r   c           
         t          |          r|                    d          }| j        5  | j                            | j        |t          |          t          j        |pd          t          j	        |pd                    }ddd           n# 1 swxY w Y   |st                      t          | |                                          S )a  Evaluate JavaScript code in the V8 isolate.

        Side effects from the JavaScript evaluation is persisted inside a context
        (meaning variables set are kept for the next evaluations).

        The JavaScript value returned by the last expression in `code` is converted
        to a Python value and returned by this method. Only primitive types are
        supported (numbers, strings, buffers...). Use the :py:meth:`.execute` method to return
        more complex types such as arrays or objects.

        The evaluation can be interrupted by an exception for several reasons: a limit
        was reached, the code could not be parsed, a returned value could not be
        converted to a Python value.

        :param code: JavaScript code
        :param timeout: number of milliseconds after which the execution is interrupted
        :param max_memory: hard memory limit after which the execution is interrupted
        utf8r   N)r\   r   r   r*   rk   r   lenra   ri   rj   rF   MiniRacerValue	to_python)rO   codetimeout
max_memoryress        r   evalzMiniRacer.eval   s   * d 	';;v&&DY 	M 	M(**48+/+.t99+1>',Q+G+G+1?:?+K+K	M MC	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M  	*')))dC((22444s   ABBBc                     d|z  }|                      |||          }t          |          s/t          d                    t	          |                              | j                            |          S )a  Helper to evaluate a JavaScript expression and return composite types.

        Returned value is serialized to JSON inside the V8 isolate and deserialized
        using :py:attr:`.json_impl`.

        :param expr: JavaScript expression
        :param timeout: number of milliseconds after which the execution is interrupted
        :param max_memory: hard memory limit after which the execution is interrupted
        z+JSON.stringify((function(){return (%s)})())r   r   zUnexpected return value type {})r   r\   
ValueErrorr    type	json_implloads)rO   exprr   r   wrapped_exprrets         r   executezMiniRacer.execute   sn     FLiig*iMM# 	S?FFtCyyQQRRR~##C(((r   c                    |                     dd          }|                     dd          }|                     dd          }| j                            |d|          }d                    ||          }|                     |||	          S )
a%  Helper to call a JavaScript function and return compositve types.

        The `expr` argument refers to a JavaScript function in the current V8
        isolate context. Further positional arguments are serialized using the JSON
        implementation :py:attr:`.json_impl` and passed to the JavaScript function
        as arguments.

        Returned value is serialized to JSON inside the V8 isolate and deserialized
        using :py:attr:`.json_impl`.

        :param str expr: JavaScript expression referring to a function
        :param encoder: Custom JSON encoder
        :type encoder: JSONEncoder or None
        :param int timeout: number of milliseconds after which the execution is interrupted
        :param int max_memory: hard memory limit after which the execution is interrupted
        encoderNr   r   ),:)
separatorsclsz{expr}.apply(this, {json_args}))r   	json_argsr   )getr   dumpsr    r   )	rO   r   argskwargsr   r   r   r   jss	            r   callzMiniRacer.call   s    $ **Y--**Y--ZZd33
N((*'(RR	/66DI6VV||BJ|GGGr   c                 F    | j                             | j        |           dS )a  Set a soft memory limit on this V8 isolate.

        The Garbage Collection will use a more aggressive strategy when
        the soft limit is reached but the execution will not be stopped.

        :param int limit: memory limit in bytes or 0 to reset the limit
        N)r*   rs   r   )rO   limits     r   set_soft_memory_limitzMiniRacer.set_soft_memory_limit  s$     	))$(E:::::r   c                 @    | j                             | j                  S )zCReturn true if the soft memory limit was reached on the V8 isolate.)r*   rt   r   rR   s    r   was_soft_memory_limit_reachedz'MiniRacer.was_soft_memory_limit_reached  s     x44TX>>>r   c                 D    | j                             | j                   dS )z7Ask the V8 isolate to collect memory more aggressively.N)r*   rq   r   rR   s    r   low_memory_notificationz!MiniRacer.low_memory_notification!  s     ++DH55555r   c                    | j         5  | j                            | j                  }ddd           n# 1 swxY w Y   |sddddddS | j                            t          | |                                                    S )z&Return the V8 isolate heap statistics.Nr   )total_physical_sizeused_heap_sizetotal_heap_sizetotal_heap_size_executableheap_size_limit)r   r*   rp   r   r   r   r   r   rO   r   s     r   
heap_statszMiniRacer.heap_stats%  s     Y 	3 	3(((22C	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3  	()#$$%/0$%   ~##N4$=$=$G$G$I$IJJJ    488c                     | j         5  | j                            | j                  }ddd           n# 1 swxY w Y   t	          | |                                          S )z)Return a snapshot of the V8 isolate heap.N)r   r*   rr   r   r   r   r   s     r   heap_snapshotzMiniRacer.heap_snapshot6  s     Y 	6 	6(++DH55C	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 dC((22444r   c                 F    | j                             | j        |           d S rL   )r*   rn   r   r   s     r   _freezMiniRacer._free>  s"    tx-----r   c                 X    | j                             t          | dd                      d S )Nr   )r*   ro   r!   rR   s    r   __del__zMiniRacer.__del__A  s*      ud!;!;<<<<<r   )NN)r6   r7   r8   r9   jsonr   r   r*   rP   propertyr   r   r   r   r   r   r   r   r   r   r   r:   r   r   rz   rz      s         I#$H
C% % % 6 6 X6!5 !5 !5 !5F) ) ) )"H H H4	; 	; 	;? ? ?
6 6 6K K K"5 5 5. . .= = = = =r   rz   c                   Z    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdS )MiniRacerTypesz`MiniRacer types identifier

    Note: it needs to be coherent with mini_racer_extension.cc.
    r         r0                  	   
   d   e   f               N)r6   r7   r8   r9   invalidnullboolintegerdoublestr_utf8arrayhashdatesymbolobjectfunctionshared_array_bufferarray_bufferexecute_exceptionparse_exceptionoom_exceptiontimeout_exceptionr:   r   r   r   r   I  sy         
 GDDGFHEDDFFHLOMr   r   c                   B    e Zd Zdej        fdej        fdej        fgZdS )rm   r[   r   r   N)r6   r7   r8   ra   rf   rh   rj   _fields_r:   r   r   rm   rm   e  s3        &/*&(*HHHr   rm   c                   &    e Zd Zdej        fgZdZdS )ArrayBufferBytebr   N)r6   r7   r8   ra   c_ubyter   _pack_r:   r   r   r   r   k  s$         fn%&HFFFr   r   c                   t    e Zd Zd Zd Zed             Zed             Zed             Zd Z	d Z
d Zd	 Zd
S )r   c                 "    || _         || _        d S rL   )r   ptr)rO   r   r   s      r   rP   zMiniRacerValue.__init__t  s    r   c                 D    t          |                                           S rL   )strr   rR   s    r   __str__zMiniRacerValue.__str__x  s    4>>##$$$r   c                 $    | j         j        j        S rL   )r   contentsr   rR   s    r   r   zMiniRacerValue.type{  s    x %%r   c                 $    | j         j        j        S rL   )r   r   r[   rR   s    r   r[   zMiniRacerValue.value  s    x &&r   c                 $    | j         j        j        S rL   )r   r   r   rR   s    r   r   zMiniRacerValue.len  s    x $$r   c                     t           j                            | j        j                  }t           j                            |          j        S rL   )ra   rc   from_bufferr   r   c_doubler[   )rO   r   s     r   _double_valuezMiniRacerValue._double_value  s5    o))$(*;<<**3//55r   c                 @   | j         t          j        k    r-t          j        | j                  j        }t          |          | j         t          j        k    rBt          j        | j                  j        }t          |	                    dd                    | j         t          j
        k    r-t          j        | j                  j        }t          |          | j         t          j        k    r-t          j        | j                  j        }t          |          d S )Nr}   replace)errors)r   r   r   ra   rc   r[   r<   r   r?   decoder   rA   r   rD   )rO   msgs     r   _raise_from_errorz MiniRacerValue._raise_from_error  s    9666/$*--3C"3'''Y.:::/$*--3C!#**WY*"G"GHHHY.666/$*--3C %%%Y.:::/$*--3C$S))) ;:r   c                    |                                   d }| j        }|t          j        k    rd }n|t          j        k    r| j        dk    }n|t          j        k    r(| j        }|d}nt          j        |          j        }n|t          j	        k    r| 
                                }n|t          j        k    rnt          j        | j                  }t          j        |t          j        t          j                            }|d| j                                     d          }n|t          j        k    rt'                      }n|t          j        k    r7| 
                                }t*          j                            |dz            }n|t          j        k    rt1                      }n|t          j        k    s|t          j        k    r>t6          | j        z                      | j                  }| |_        t=          |          }n2|t          j        k    rtA          | j                  S tC                      |S )Nr   r   r   g     @@)"r   r   r   r   r   r[   r   ra   c_int32r   r   r   rc   castrl   c_charr   r   r   rU   r   datetimeutcfromtimestampr   rW   r   r   r   from_address_origin
memoryviewr   rJ   rF   )rO   resulttypvalbufr   	timestampcdatas           r   r   zMiniRacerValue.to_python  s      i.%%%FFN'''Z1_FFN****C{,,2N)))''))FFN+++/$*--C+c6>&-#@#@AAC48_++F33FFN+++\\FFN'''**,,I&77	E8IJJFFN)))ZZFFN666#A\:\:\$tx/==djIIE EM&&FFN)))DJ'''')))r   c                 D    | j                             | j                   d S rL   )r   r   r   rR   s    r   r   zMiniRacerValue.__del__  s    tx     r   N)r6   r7   r8   rP   r   r   r   r[   r   r   r   r   r   r:   r   r   r   r   r  s          % % % & & X& ' ' X' % % X%6 6 6* * *% % %N! ! ! ! !r   r   ).ra   r  r   r   r   r   r   r   machiner   r$   ImportErrorr   r.   r^   r"   basenameEXTENSION_NAMEversion_infounicoderZ   typingr1   r2   r   	Exceptionr4   r<   r?   rA   rD   rF   rH   r   rJ   rU   rW   r\   rx   rz   StrictMiniRacerr   	Structurerm   r   r   r:   r   r   <module>r     s      				  



        
(


   MMM    8 |,,5C5O!!.111UYALL$$$$$$$$L$ $ $ $ $Y $ $ $* * * * *- * * *, , , , ,, , , ,2 2 2 2 2_ 2 2 2* * * * * * * *2 2 2 2 22 2 2 2; ; ; ; ;5 ; ; ;    v              v   + + +"% % %PX= X= X= X= X= X= X= X=x     V   8* * * * *6+ * * *    f&   O! O! O! O! O!V O! O! O! O! O!s   6 A A 