17 Unsafe Operations
(require racket/unsafe/ops) | package: base |
All functions and forms provided by racket/base and racket check their arguments to ensure that the arguments conform to contracts and other constraints. For example, vector-ref checks its arguments to ensure that the first argument is a vector, that the second argument is an exact integer, and that the second argument is between 0 and one less than the vector’s length, inclusive.
Functions provided by racket/unsafe/ops are unsafe. They have certain constraints, but the constraints are not checked, which allows the system to generate and execute faster code. If arguments violate an unsafe function’s constraints, the function’s behavior and result is unpredictable, and the entire system can crash or become corrupted.
All of the exported bindings of racket/unsafe/ops are protected in the sense of protect-out, so access to unsafe operations can be prevented by adjusting the code inspector (see Code Inspectors).
17.1 Unsafe Numeric Operations
函数
(unsafe-fx+ a ...) → fixnum?
a : fixnum?
函数
(unsafe-fx- a b ...) → fixnum?
a : fixnum? b : fixnum?
函数
(unsafe-fx* a ...) → fixnum?
a : fixnum?
函数
(unsafe-fxquotient a b) → fixnum?
a : fixnum? b : fixnum?
函数
(unsafe-fxremainder a b) → fixnum?
a : fixnum? b : fixnum?
函数
(unsafe-fxmodulo a b) → fixnum?
a : fixnum? b : fixnum?
函数
(unsafe-fxabs a) → fixnum?
a : fixnum?
修改于package base的7.0.0.13版本:Allow zero or more arguments for unsafe-fx+ and unsafe-fx* and allow one or more arguments for unsafe-fx-.
函数
(unsafe-fxand a ...) → fixnum?
a : fixnum?
函数
(unsafe-fxior a ...) → fixnum?
a : fixnum?
函数
(unsafe-fxxor a ...) → fixnum?
a : fixnum?
函数
(unsafe-fxnot a) → fixnum?
a : fixnum?
函数
(unsafe-fxlshift a b) → fixnum?
a : fixnum? b : fixnum?
函数
(unsafe-fxrshift a b) → fixnum?
a : fixnum? b : fixnum?
修改于package base的7.0.0.13版本:Allow zero or more arguments for unsafe-fxand, unsafe-fxior, and unsafe-fxxor.
函数
(unsafe-fx= a b ...) → boolean?
a : fixnum? b : fixnum?
函数
(unsafe-fx< a b ...) → boolean?
a : fixnum? b : fixnum?
函数
(unsafe-fx> a b ...) → boolean?
a : fixnum? b : fixnum?
函数
(unsafe-fx<= a b ...) → boolean?
a : fixnum? b : fixnum?
函数
(unsafe-fx>= a b ...) → boolean?
a : fixnum? b : fixnum?
函数
(unsafe-fxmin a b ...) → fixnum?
a : fixnum? b : fixnum?
函数
(unsafe-fxmax a b ...) → fixnum?
a : fixnum? b : fixnum?
修改于package base的7.0.0.13版本:Allow one or more argument, instead of allowing just two.
函数
(unsafe-fl+ a ...) → flonum?
a : flonum?
函数
(unsafe-fl- a b ...) → flonum?
a : flonum? b : flonum?
函数
(unsafe-fl* a ...) → flonum?
a : flonum?
函数
(unsafe-fl/ a b ...) → flonum?
a : flonum? b : flonum?
函数
(unsafe-flabs a) → flonum?
a : flonum?
修改于package base的7.0.0.13版本:Allow zero or more arguments for unsafe-fl+ and unsafe-fl* and one or more arguments for unsafe-fl- and unsafe-fl/.
函数
(unsafe-fl= a b ...) → boolean?
a : flonum? b : flonum?
函数
(unsafe-fl< a b ...) → boolean?
a : flonum? b : flonum?
函数
(unsafe-fl> a b ...) → boolean?
a : flonum? b : flonum?
函数
(unsafe-fl<= a b ...) → boolean?
a : flonum? b : flonum?
函数
(unsafe-fl>= a b ...) → boolean?
a : flonum? b : flonum?
函数
(unsafe-flmin a b ...) → flonum?
a : flonum? b : flonum?
函数
(unsafe-flmax a b ...) → flonum?
a : flonum? b : flonum?
修改于package base的7.0.0.13版本:Allow one or more argument, instead of allowing just two.
函数
(unsafe-flround a) → flonum?
a : flonum?
函数
(unsafe-flfloor a) → flonum?
a : flonum?
函数
(unsafe-flceiling a) → flonum?
a : flonum?
函数
(unsafe-fltruncate a) → flonum?
a : flonum?
函数
(unsafe-flsin a) → flonum?
a : flonum?
函数
(unsafe-flcos a) → flonum?
a : flonum?
函数
(unsafe-fltan a) → flonum?
a : flonum?
函数
(unsafe-flasin a) → flonum?
a : flonum?
函数
(unsafe-flacos a) → flonum?
a : flonum?
函数
(unsafe-flatan a) → flonum?
a : flonum?
函数
(unsafe-fllog a) → flonum?
a : flonum?
函数
(unsafe-flexp a) → flonum?
a : flonum?
函数
(unsafe-flsqrt a) → flonum?
a : flonum?
函数
(unsafe-flexpt a b) → flonum?
a : flonum? b : flonum?
函数
→
(and/c complex? (lambda (c) (flonum? (real-part c))) (lambda (c) (flonum? (imag-part c)))) a : flonum? b : flonum?
函数
(unsafe-flreal-part a) → flonum?
a :
(and/c complex? (lambda (c) (flonum? (real-part c))) (lambda (c) (flonum? (imag-part c))))
函数
(unsafe-flimag-part a) → flonum?
a :
(and/c complex? (lambda (c) (flonum? (real-part c))) (lambda (c) (flonum? (imag-part c))))
函数
(unsafe-fx->fl a) → flonum?
a : fixnum?
函数
(unsafe-fl->fx a) → fixnum?
a : flonum?
函数
(unsafe-flrandom rand-gen) → (and flonum? (>/c 0) (</c 1))
rand-gen : pseudo-random-generator?
17.2 Unsafe Character Operations
函数
(unsafe-char=? a b ...) → boolean?
a : char? b : char?
函数
(unsafe-char<? a b ...) → boolean?
a : char? b : char?
函数
(unsafe-char>? a b ...) → boolean?
a : char? b : char?
函数
(unsafe-char<=? a b ...) → boolean?
a : char? b : char?
函数
(unsafe-char>=? a b ...) → boolean?
a : char? b : char?
函数
(unsafe-char->integer a) → fixnum?
a : char?
添加于package base的7.0.0.14版本。
17.3 Unsafe Data Extraction
函数
(unsafe-car p) → any/c
p : pair?
函数
(unsafe-cdr p) → any/c
p : pair?
函数
(unsafe-mcar p) → any/c
p : mpair?
函数
(unsafe-mcdr p) → any/c
p : mpair?
函数
(unsafe-set-mcar! p v) → void?
p : mpair? v : any/c
函数
(unsafe-set-mcdr! p v) → void?
p : mpair? v : any/c
函数
(unsafe-list-ref lst pos) → any/c
lst : pair? pos : (and/c exact-nonnegative-integer? fixnum?)
函数
(unsafe-list-tail lst pos) → any/c
lst : any/c pos : (and/c exact-nonnegative-integer? fixnum?)
函数
(unsafe-unbox b) → fixnum?
b : box?
函数
(unsafe-set-box! b k) → void?
b : box? k : fixnum?
函数
(unsafe-unbox* v) → any/c
v : (and/c box? (not/c impersonator?))
函数
(unsafe-set-box*! v val) → void?
v : (and/c box? (not/c impersonator?)) val : any/c
函数
(unsafe-box*-cas! loc old new) → boolean?
loc : box? old : any/c new : any/c
函数
(unsafe-vector-length v) → fixnum?
v : vector?
函数
(unsafe-vector-ref v k) → any/c
v : vector? k : fixnum?
函数
(unsafe-vector-set! v k val) → void?
v : vector? k : fixnum? val : any/c
函数
(unsafe-vector*-length v) → fixnum?
v : (and/c vector? (not/c impersonator?))
函数
(unsafe-vector*-ref v k) → any/c
v : (and/c vector? (not/c impersonator?)) k : fixnum?
函数
(unsafe-vector*-set! v k val) → void?
v : (and/c vector? (not/c impersonator?)) k : fixnum? val : any/c
函数
(unsafe-vector*-cas! v k old-val new-val) → boolean?
v : (and/c vector? (not/c impersonator?)) k : fixnum? old-val : any/c new-val : any/c
A vector’s size can never be larger than a fixnum, so even vector-length always returns a fixnum.
修改于package base的6.11.0.2版本:Added unsafe-vector*-cas!.
函数
(unsafe-string-length str) → fixnum?
str : string?
函数
(unsafe-string-ref str k)
→ (and/c char? (lambda (ch) (<= 0 (char->integer ch) 255))) str : string? k : fixnum?
函数
(unsafe-string-set! str k ch) → void?
str : (and/c string? (not/c immutable?)) k : fixnum? ch : char?
函数
(unsafe-bytes-length bstr) → fixnum?
bstr : bytes?
函数
(unsafe-bytes-ref bstr k) → byte?
bstr : bytes? k : fixnum?
函数
(unsafe-bytes-set! bstr k b) → void?
bstr : (and/c bytes? (not/c immutable?)) k : fixnum? b : byte?
函数
v : fxvector?
函数
(unsafe-fxvector-ref v k) → fixnum?
v : fxvector? k : fixnum?
函数
(unsafe-fxvector-set! v k x) → void?
v : fxvector? k : fixnum? x : fixnum?
函数
v : flvector?
函数
(unsafe-flvector-ref v k) → flonum?
v : flvector? k : fixnum?
函数
(unsafe-flvector-set! v k x) → void?
v : flvector? k : fixnum? x : flonum?
函数
(unsafe-f64vector-ref vec k) → flonum?
vec : f64vector? k : fixnum?
函数
(unsafe-f64vector-set! vec k n) → void?
vec : f64vector? k : fixnum? n : flonum?
函数
(unsafe-s16vector-ref vec k) → (integer-in -32768 32767)
vec : s16vector? k : fixnum?
函数
(unsafe-s16vector-set! vec k n) → void?
vec : s16vector? k : fixnum? n : (integer-in -32768 32767)
函数
(unsafe-u16vector-ref vec k) → (integer-in 0 65535)
vec : u16vector? k : fixnum?
函数
(unsafe-u16vector-set! vec k n) → void?
vec : u16vector? k : fixnum? n : (integer-in 0 65535)
函数
(unsafe-struct-ref v k) → any/c
v : any/c k : fixnum?
函数
(unsafe-struct-set! v k val) → void?
v : any/c k : fixnum? val : any/c
函数
(unsafe-struct*-ref v k) → any/c
v : (not/c impersonator?) k : fixnum?
函数
(unsafe-struct*-set! v k val) → void?
v : (not/c impersonator?) k : fixnum? val : any/c
函数
(unsafe-struct*-cas! v k old-val new-val) → boolean?
v : (not/c impersonator?) k : fixnum? old-val : any/c new-val : any/c
修改于package base的6.11.0.2版本:Added unsafe-struct*-cas!.
Each unsafe ...-first and ...-next procedure may return, instead of a number index, an internal representation of a view into the hash structure, enabling faster iteration. The result of these ...-first and ...-next functions should be given as pos to the corresponding unsafe accessor functions.
If the pos provided to an accessor function for a mutable hash was formerly a valid hash index but is no longer a valid hash index for hash, and if bad-index-v is not provided, then the exn:fail:contract exception is raised. No behavior is specified for a pos that was never a valid hash index for hash. Note that bad-index-v argument is technically not useful for the unsafe-immutable-hash-iterate- functions, since an index cannot become invalid for an immutable hash.
添加于package base的6.4.0.6版本。
修改于7.0.0.10版本:Added the optional bad-index-v argument.
17.4 Unsafe Extflonum Operations
函数
(unsafe-extfl+ a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-extfl- a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-extfl* a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-extfl/ a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-extflabs a) → extflonum?
a : extflonum?
函数
(unsafe-extfl= a b) → boolean?
a : extflonum? b : extflonum?
函数
(unsafe-extfl< a b) → boolean?
a : extflonum? b : extflonum?
函数
(unsafe-extfl> a b) → boolean?
a : extflonum? b : extflonum?
函数
(unsafe-extfl<= a b) → boolean?
a : extflonum? b : extflonum?
函数
(unsafe-extfl>= a b) → boolean?
a : extflonum? b : extflonum?
函数
(unsafe-extflmin a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-extflmax a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-extflround a) → extflonum?
a : extflonum?
函数
(unsafe-extflfloor a) → extflonum?
a : extflonum?
函数
a : extflonum?
函数
a : extflonum?
函数
(unsafe-extflsin a) → extflonum?
a : extflonum?
函数
(unsafe-extflcos a) → extflonum?
a : extflonum?
函数
(unsafe-extfltan a) → extflonum?
a : extflonum?
函数
(unsafe-extflasin a) → extflonum?
a : extflonum?
函数
(unsafe-extflacos a) → extflonum?
a : extflonum?
函数
(unsafe-extflatan a) → extflonum?
a : extflonum?
函数
(unsafe-extfllog a) → extflonum?
a : extflonum?
函数
(unsafe-extflexp a) → extflonum?
a : extflonum?
函数
(unsafe-extflsqrt a) → extflonum?
a : extflonum?
函数
(unsafe-extflexpt a b) → extflonum?
a : extflonum? b : extflonum?
函数
(unsafe-fx->extfl a) → extflonum?
a : fixnum?
函数
(unsafe-extfl->fx a) → fixnum?
a : extflonum?
函数
v : extflvector?
函数
(unsafe-extflvector-ref v k) → extflonum?
v : extflvector? k : fixnum?
函数
(unsafe-extflvector-set! v k x) → void?
v : extflvector? k : fixnum? x : extflonum?
17.5 Unsafe Impersonators and Chaperones
函数
(unsafe-impersonate-procedure proc replacement-proc prop prop-val ... ...) → (and/c procedure? impersonator?) proc : procedure? replacement-proc : procedure? prop : impersonator-property? prop-val : any
If proc is itself an impersonator that is derived from impersonate-procedure* or chaperone-procedure*, beware that replacement-proc will not be able to call it correctly. Specifically, the impersonator produced by unsafe-impersonate-procedure will not get passed to a wrapper procedure that was supplied to impersonate-procedure* or chaperone-procedure* to generate proc.
Finally, unlike impersonate-procedure, unsafe-impersonate-procedure does not specially handle impersonator-prop:application-mark as a prop.
The unsafety of unsafe-impersonate-procedure is limited to the above differences from impersonate-procedure. The contracts on the arguments of unsafe-impersonate-procedure are checked when the arguments are supplied.
(define log1-args '()) (define log1-results '()) (define wrap-f1 (λ (f) (impersonate-procedure f (λ (arg) (set! log1-args (cons arg log1-args)) (values (λ (res) (set! log1-results (cons res log1-results)) res) arg))))) (define log2-args '()) (define log2-results '()) (define wrap-f2 (λ (f) (unsafe-impersonate-procedure f (λ (arg) (set! log2-args (cons arg log2-args)) (let ([res (f arg)]) (set! log2-results (cons res log2-results)) res)))))
添加于package base的6.4.0.4版本。
函数
(unsafe-chaperone-procedure proc wrapper-proc prop prop-val ... ...) → (and/c procedure? chaperone?) proc : procedure? wrapper-proc : procedure? prop : impersonator-property? prop-val : any
添加于package base的6.4.0.4版本。
函数
(unsafe-impersonate-vector vec replacement-vec prop prop-val ... ...) → (and/c vector? impersonator?) vec : vector? replacement-vec : (and/c vector? (not/c impersonator?)) prop : impersonator-property? prop-val : any/c
The result of unsafe-impersonate-vector is an impersonator of vec.
添加于package base的6.9.0.2版本。
函数
(unsafe-chaperone-vector vec replacement-vec prop prop-val ... ...) → (and/c vector? chaperone?) vec : vector? replacement-vec : (and/c vector? (not/c impersonator?)) prop : impersonator-property? prop-val : any/c
添加于package base的6.9.0.2版本。
17.6 Unsafe Undefined
(require racket/unsafe/undefined) | package: base |
The constant unsafe-undefined is used internally as a placeholder value. For example, it is used by letrec as a value for a variable that has not yet been assigned a value. Unlike the undefined value exported by racket/undefined, however, the unsafe-undefined value should not leak as the result of a safe expression, and it should not be passed as an optional argument to a procedure (because it may count as “no value provided”). Expression results that potentially produce unsafe-undefined can be guarded by check-not-unsafe-undefined, so that an exception can be raised instead of producing an undefined value.
The unsafe-undefined value is always eq? to itself.
添加于package base的6.0.1.2版本。
修改于6.90.0.29版本:Procedures with optional arguments
sometimes use the unsafe-undefined
value internally to mean “no argument supplied.”
See above for important constraints on the use of unsafe-undefined.
函数
(check-not-unsafe-undefined v sym)
→ (and/c any/c (not/c (one-of/c unsafe-undefined))) v : any/c sym : symbol?
函数
v : any/c
When a field access would otherwise produce unsafe-undefined or when a field assignment would replace unsafe-undefined, the exn:fail:contract exception is raised.
The chaperone’s field-assignment check is disabled whenever
(continuation-mark-set-first #f prop:chaperone-unsafe-undefined) returns unsafe-undefined.
Thus, a field-initializing assignment—
The property value should be a list of symbols used as field names, but the list should be in reverse order of the structure’s fields. When a field access or assignment would produce or replace unsafe-undefined, the exn:fail:contract:variable exception is raised if a field name is provided by the structure property’s value, otherwise the exn:fail:contract exception is raised.