
    OJniG                         d dl mZm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	d dl
Z
d dlZd dlZd dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ  G d de      Zd	 Zd
 ZddZd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'y)    )PopenPIPEN)ProcessExitedWithNonZeroStatusProgramError)AbstractRuntime)AbstractRuntimeContext)encode_unicode_codepointsc                   X    e Zd ZdZddZd Zed        Zd ZddZ	d Z
 G d	 d
e      Zy)ExternalRuntimez/Runtime to execute codes with external command.c                     || _         t        |t              r|g}|| _        || _        || _        || _        | j                         d u| _        y N)	_name
isinstancestr_command_runner_source	_encoding	_tempfile_binary
_available)selfnamecommandrunner_sourceencodingtempfiles         I/home/uftp/myenv/lib/python3.12/site-packages/execjs/_external_runtime.py__init__zExternalRuntime.__init__   sI    
gs#iG+!!,,.4    c                 b    dj                  t        |       j                  | j                        S )Nz{class_name}({runtime_name}))
class_nameruntime_name)formattype__name__r   r   s    r   __str__zExternalRuntime.__str__&   s.    -44Dz** 5 
 	
r   c                     | j                   S r   )r   r&   s    r   r   zExternalRuntime.name,   s    zzr   c                     | j                   S r   )r   r&   s    r   is_availablezExternalRuntime.is_available0   s    r   Nc                 @    | j                  | ||| j                        S )N)cwdr   )Contextr   )r   sourcer,   s      r   _compilezExternalRuntime._compile3   s    ||D&cDNN|KKr   c                 f    t        | d      st        | j                        | _        | j                  S )N_binary_cache)hasattr_whichr   r1   r&   s    r   r   zExternalRuntime._binary6   s)    t_-!'!6D!!!r   c                   J    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zy)ExternalRuntime.ContextNc                 <    || _         || _        || _        || _        y r   )_runtime_source_cwdr   )r   runtimer.   r,   r   s        r   r   z ExternalRuntime.Context.__init__>   s    #DM!DLDI%DNr   c                 6    | j                   j                         S r   )r7   r*   r&   s    r   r*   z$ExternalRuntime.Context.is_availableD   s    ==--//r   c                     |j                         sd}ndt        j                  |d      z   dz   }dj                  |      }| j	                  |      S )Nz''z'('+T)ensure_asciiz+')'zreturn eval({data}))data)stripjsondumpsr#   exec_)r   r.   r>   codes       r   _evalzExternalRuntime.Context._evalG   sL    <<>

6 EEN(//T/:D::d##r   c                     | j                   r| j                   dz   |z   }| j                  r| j                  |      }n| j                  |      }| j	                  |      S )N
)r8   r   _exec_with_tempfile_exec_with_pipe_extract_result)r   r.   outputs      r   _exec_zExternalRuntime.Context._exec_P   sT    ||,v5~~11&9--f5''//r   c                 p    t        j                  |      }| j                  dj                  ||            S )Nz {identifier}.apply(this, {args}))
identifierargs)r@   rA   rD   r#   )r   rM   rN   s      r   _callzExternalRuntime.Context._callZ   s2    ::d#D::@GGS]dhGijjr   c                    | j                   j                         }d }	 t        |t        t        t        | j                  d      }| j                  |      }t        j                  r#|j                  t        j                               }|j                  |      \  }}|j                         }~| j                  |||       |S # ~w xY w)NT)stdinstdoutstderrr,   universal_newlines)input)r7   r   r   r   r9   r/   sixPY2encodesysgetfilesystemencodingcommunicatewait_fail_on_non_zero_status)r   r.   cmdprU   
stdoutdata
stderrdatarets           r   rH   z'ExternalRuntime.Context._exec_with_pipe^   s    --'')CA#T$tgklf-77!LL)B)B)DEE)*U)C&
Jffh))#z:F s   BC Cc                    t        j                  dd      \  }}t        j                  |       	 t	        j
                  |d| j                  j                        5 }|j                  | j                  |             d d d        | j                  j                         |gz   }d }	 t        |t        t        | j                  d      }|j                         \  }}|j                         }	~| j!                  |	||       |t        j"                  |       S # 1 sw Y   xY w# ~w xY w# t        j"                  |       w xY w)Nexecjsz.js)prefixsuffixzw+)r   T)rR   rS   r,   rT   )r   mkstemposcloseioopenr7   r   writer/   r   r   r   r9   r[   r\   r]   remove)
r   r.   fdfilenamefpr^   r_   r`   ra   rb   s
             r   rG   z+ExternalRuntime.Context._exec_with_tempfileo   s    %--XeLNRHHRL$WWXtdmm6M6MN 4RTHHT]]6234mm++-
:c$t_cdA-.]]_*J
&&(C--c:zJ!		(#4 4 
 		(#s<   ,D) !D>(D) 'AD# ,D) D D) #D&&D) )E c                 *    |dk7  rt        |||      y )Nr   )statusrR   rS   )r   )r   rr   r`   ra   s       r   r]   z0ExternalRuntime.Context._fail_on_non_zero_status   s    {4F:^hii r   c                     | j                   j                  }fdfdt        j                  ddj	                  d D              }t        j                  |fd|      }|S )Nc                       S r    r.   s   r   <lambda>z2ExternalRuntime.Context._compile.<locals>.<lambda>   s    V r   c                  L    t        j                  dt               z   dz         S )Nz(function(){ z })())r@   rA   r	   rv   s   r   rw   z2ExternalRuntime.Context._compile.<locals>.<lambda>   s(    TZZ#-f56. r   )z	#{source}z#{encoded_source}z#{json2_source}|c              3   F   K   | ]  }t        j                  |        y wr   )reescape).0ks     r   	<genexpr>z3ExternalRuntime.Context._compile.<locals>.<genexpr>   s     Bryy|Bs   !c                 6     | j                  d                S )Nr   )group)mreplacementss    r   rw   z2ExternalRuntime.Context._compile.<locals>.<lambda>   s    6Nl1771:6N6P r   )r7   r   _json2_json2_sourcejoinr{   sub)r   r.   r   patternr   s    `  @r   r/   z ExternalRuntime.Context._compile   s^     MM88M ,&
 $*#7#7L hhB\BBGFF7,PR_`M  r   c                     |j                  dd      j                  dd      }|j                  d      d   }t        j                  |      }t	        |      dk(  r|d   d g}|\  }}|dk(  r|S t        |      )Nz
rF      r   ok)replacesplitr@   loadslenr   )r   rJ   output_last_linerb   rr   values         r   rI   z'ExternalRuntime.Context._extract_result   sz    ^^FD199$EF%||D1"5**-.C3x1}1vtnMFE~"5))r   ) NF)r%   
__module____qualname__r   r*   rD   rK   rO   rH   rG   r]   r/   rI   ru   r   r   r-   r5   ;   s6    	&	0	$	0	k	"	$*	j	!&	*r   r-   )utf8Fr   )r%   r   r   __doc__r   r'   propertyr   r*   r/   r   r   r-   ru   r   r   r   r      sB    9	5
  L"
l*( l*r   r   c                  0    t        j                         dk(  S )	protectedWindows)platformsystemru   r   r   _is_windowsr      s    ??	))r   c                     t        | t        j                        r| S | j                  t	        j
                               S )r   )r   rV   	text_typedecoderY   rZ   )ss    r   _decode_if_not_textr      s-    !S]]#88C--/00r   c                    t        t        j                  j                  dd            j	                  t        j
                        }|D ]  }|D ]  }t        j                  j                  || |z         }	 t        j                  |      }t        j                  |j                        s\t        j                  |j                        dz  s|c c S   y# t        j                  $ r Y w xY w)r   PATHr   I   N)r   rh   environgetr   pathseppathr   staterrorS_ISREGst_modeS_IMODE)progpathextpathlistdirextro   sts          r   _find_executabler      s    "2::>>&"#=>DDRZZPH   	 Cww||C4HWWX& ||BJJ'T\\"**-E-M	   	 88 s   5CC-,C-c                 4   t        | t              r| g} t        |       } | d   }| dd }t               rSt	        t
        j                  j                  dd            }t        ||j                  t
        j                              }nt        |      }|sy|g|z   S )r   r   r   NPATHEXTr   )r   r   listr   r   rh   r   r   r   r   r   )r   r   rN   r   r   s        r   r3   r3      s    '3)7mG1:D12;D}%bjjnnY&CDgmmBJJ&?@%6D=r   c                  N    t               } | j                         r| S t               S r   )	node_noder*   node_nodejs)rs    r   noder      s     A~~=r   c                  >    t        ddgdt        j                        S )NNode.js (V8)r   UTF-8r   r   r   r   r   _runner_sourcesNoderu   r   r   r   r      s#    %**	 r   c                  >    t        ddgdt        j                        S )Nr   nodejsr   r   r   ru   r   r   r   r      s#    
%**	 r   c                  >    t        ddgt        j                  d      S )NJavaScriptCorezL/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jscTr   r   r   r   )r   r   r   ru   r   r   jscr      s$    _`%44	 r   c                  >    t        ddgt        j                  d      S )NSpiderMonkeyjsTr   )r   r   r   ru   r   r   spidermonkeyr      s#    %22	 r   c                  B    t        dg ddt        j                  d      S )NJScript)cscriptz//E:jscriptz//NologoasciiT)r   r   r   r   r   )r   r   r   ru   r   r   jscriptr     s$    6%-- r   c                  >    t        ddgt        j                  d      S )N	PhantomJS	phantomjsTr   )r   r   r   ru   r   r   r   r     s#    %//	 r   c                  >    t        ddgt        j                  d      S )NSlimerJSslimerjsTr   )r   r   r   ru   r   r   r   r     s#    %..	 r   c                  >    t        ddgt        j                  d      S )NNashornjjsTr   )r   r   r   ru   r   r   nashornr      s#    %--	 r   ))r   )(
subprocessr   r   rj   r@   rh   os.pathr   r{   r   rY   r   rV   execjs._json2r   execjs._runner_sourcesr   execjs._exceptionsr   r   execjs._abstract_runtimer    execjs._abstract_runtime_contextr   execjs._miscr	   r   r   r   r   r3   r   r   r   r   r   r   r   r   r   ru   r   r   <module>r      s    " 	  	   	  
  
  0
 5 C 2N*o N*b*
1 &r   